简体   繁体   English

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

[英]ASPX auth cookie expiration time is always 30 minutes

I have set the the cookie expiration time to 1 month but when I look the expiration timeout of .ASPXAUTH cookie in browser it says 30 minutes ahead from now. 我已将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);

Can you let me know why the above code is behaving so, I want to change the expiration time but it is always coming 30 minutes. 你能让我知道为什么上面的代码表现如此,我想改变过期时间,但总是要30分钟。

With the advice from the other answers I got to this link. 根据其他答案的建议,我得到了这个链接。

Apparently, in ASP.NET it checks the expiration in the Web.config and doesn't take the expiration from the cookie. 显然,在ASP.NET中,它会检查Web.config中的过期,并且不会从cookie中过期。 So you need to add to the config file inside <system.web> : 所以你需要添加到<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>

Do you require to set this timeout programmatically or is it ok to set it in configuration file? 您是否需要以编程方式设置此超时,还是可以在配置文件中设置它? There is a timeout parameter, which indicates authentication cookie timeout: http://msdn.microsoft.com/en-us/library/1d3t3c61.aspx 有一个超时参数,表示身份验证cookie超时: http//msdn.microsoft.com/en-us/library/1d3t3c61.aspx

Default value of this parameter is 30 minutes. 此参数的默认值为30分钟。

Best regards, Dmitry 最好的问候,德米特里

Check you web.config file, there should be FORM entry under following element system.web -> authentication . 检查你的web.config文件,下面的元素system.web - > authentication下应该有FORM条目。

check the timeout property there, is it set to 30 minutes? 检查那里的超时属性,是否设置为30分钟?

remove this form authentication tag from there. 从那里删除此表单身份验证标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM