ASPX auth cookie到期时间始终为30分钟

时间:2012-03-27 12:18:17

标签: c# asp.net iis-7.5 asp.net-4.0

我已将Cookie过期时间设置为1个月,但当我在浏览器中查看.ASPXAUTH cookie的过期超时时,它表示从现在开始提前30分钟。

var ticket = new FormsAuthenticationTicket(1, "myname", DateTime.Now,
                                                        DateTime.Now.AddMonths(1), true, "test");
string ticketString = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketString)
                 {
                     Expires = DateTime.Now.AddMonths(1),
                     Path = FormsAuthentication.FormsCookiePath
                 };
HttpContext.Current.Response.Cookies.Add(cookie);

你能让我知道为什么上面的代码表现如此,我想改变过期时间,但总是要30分钟。

3 个答案:

答案 0 :(得分:4)

您是否需要以编程方式设置此超时,还是可以在配置文件中设置它?有一个 timeout 参数,表示身份验证Cookie超时:http://msdn.microsoft.com/en-us/library/1d3t3c61.aspx

此参数的默认值为30分钟。

祝你好运, 梅德

答案 1 :(得分:4)

根据其他答案的建议,我得到this链接。

显然,在ASP.NET中,它会检查Web.config中的过期,并且不会从cookie中过期。因此,您需要在<system.web>

中添加配置文件
<authentication mode="Forms">
  <forms
 name=".ASPXAUTH"
 loginUrl="Login.cshtml" //your login page
 defaultUrl="Default.cshtml" //your default page
 protection="All" //type of encryption
 timeout="43200" //a month in minutes
 path="/"
 requireSSL="false"
 slidingExpiration="true" //Every refresh the expiration time will reset
 cookieless="UseDeviceProfile" //Use cookies if the browser supports cookies
 domain=""
 enableCrossAppRedirects="false">
    <credentials passwordFormat="SHA1" />
  </forms>
</authentication>

答案 2 :(得分:3)

检查你的web.config文件,下面的元素system.web - &gt;下应该有FORM条目。身份验证。

检查那里的超时属性,是否设置为30分钟?

从那里删除此表单身份验证标记。