2016-10-11 64 views
0

下面是我在web.config中的身份验证配置:如何设置ASP.NET_SessionId cookie的到期日期?

<authentication mode="Forms"> 
     <forms loginUrl="~/Account/Sigin" 
      name="MYCAUTH" 
      timeout="3000" /> 
</authentication> 

我怎样才能使双方MYCAUTHASP.NET_SessionId饼干使用过期?

+0

的可能重复http://stackoverflow.com/questions/10439483/set-update-expiration-on-aspxauth-and-asp-net-sessionid-cookies ? – TheFallenOne

+0

@The_Outsider不回答 – JamesL

回答

1

试试这个:

DateTime expireDate = DateTime.Now.AddDays(30); 
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, expireDate, true, string.Empty); 
string encryptedTicket = FormsAuthentication.Encrypt(ticket); 
HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
authenticationCookie.Expires = ticket.Expiration; 
Response.Cookies.Add(authenticationCookie); 
FormsAuthentication.SetAuthCookie(userName, true); 
+0

谢谢。是否可以使用'FormsAuthentication.SetAuthCookie'?另请注意我更新了我的问题。我需要“ASP.NET_SessionId”和“MYCAUTH”的到期时间 – JamesL

+0

关闭浏览器后,ASP.NET_SessionId将过期。无论如何,这些是不同的东西。看到这篇文章的更多信息http://stackoverflow.com/questions/4939533/cookie-confusion-with-formsauthentication-setauthcookie-method – VDWWD

1
var myCookie = Request.Cookies["myCookie"]; 
    if (myCookie != null) 
    { 
     HttpCookie respCookie = new HttpCookie("myCookie", "MyValue"); 
     respCookie.Expires = DateTime.Now.AddMinutes(5); 
     Response.Cookies.Set(myCookie); 
    }