2016-10-11 91 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); 
    }