如何设置ASP.NET_SessionId cookie的截止日期?

时间:2016-10-11 20:55:56

标签: asp.net cookies session-cookies

以下是我在web.config中配置身份验证的方法:

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

如何使MYCAUTHASP.NET_SessionId Cookie都使用到期?

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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);