简体   繁体   English

ASP.NET非永久cookie(又称会话cookie)对于某些用户而言没有超时

[英]ASP.NET non-persistent cookie (aka session cookie) not time out for some users

I have an ASP.NET MVC Web project that uses non-persistent cookie (aka session cookie). 我有一个使用非持久cookie(又称为会话cookie)的ASP.NET MVC Web项目。 The requirement is that a logged in session will time out either after 30 mins of inactivity or, users closes the web browser. 要求是登录的会话将在30分钟不活动后超时,或者用户关闭Web浏览器。

The project is in production, and some users reported that they don't need to login after days of usage. 该项目正在生产中,一些用户报告说,使用几天后,无需登录。 But they have to login at the beginning of using the project. 但是他们必须在使用项目之初登录。 The defect cannot be replicated on Testing Servers so far. 到目前为止,该缺陷无法在测试服务器上复制。 Again, the defect don't happen for every user, and happens to some users after some usages. 同样,缺陷并不是每个用户发生,并且某些使用之后会发生在某些用户身上。

Below is main code: 下面是主要代码:

1) use cookie.Expires = DateTime.MinValue ; 1)使用cookie.Expires = DateTime.MinValue ; //create non-persistent cookie. //创建非永久性cookie。

2) Use FormsAuthentication.Encrpt() method to encrypt ticket 2)使用FormsAuthentication.Encrpt()方法加密票证

3) Use form element in web.config timeout attribute to validate timeout 3)使用web.config timeout属性中的表单元素来验证超时

The code above explains the main logic. 上面的代码解释了主要逻辑。

Any idea? 任何想法?

Actually, setting cookie.Expires always create persistent cookie if the date is in future or deletes the cookie if the date is in the past. 实际上,设置cookie。如果日期在将来, cookie.Expires总是创建永久性cookie,或者如果日期在过去,则删除cookie。

The line 线

cookie.Expires = DateTime.MinValue;

is somewhat confusing then. 那时有点令人困惑。 It looks like you are deleting the cookie so that when it gets to the client, it should be invalidated by the browser. 看起来您正在删除cookie,以便当它到达客户端时,浏览器应该使它无效。 The information you provide is not consistent then. 那么您提供的信息不一致。

To have non-persistent cookies, never touch the Expires property. 要具有非持久性Cookie,请不要触摸Expires属性。 Instead, set the valid date inside the FormsAuthenticationTicket . 而是在FormsAuthenticationTicket设置有效日期。 The cookie will then hit the server but the ticket inside will make it invalid. 然后,该Cookie将到达服务器,但其中的票证将使其无效。 Token validity and cookie expiry date are two unrelated dates and you should rely on the first one. 令牌有效期和cookie到期日期是两个不相关的日期,您应该依靠第一个。

My guess is that for some users, the first page can possibly be read from the browser cache. 我的猜测是,对于某些用户而言,第一页可能是从浏览器缓存中读取的。 Firefox caches aggresively and we have observed such behavior. Firefox积极地缓存,我们已经观察到这种行为。 One of possible ways to work this around is to turn of caching at the page level so that the browser is told to always ask the server for the newest version of the page and thus users have to relogin. 解决此问题的一种可能方法是在页面级别打开缓存,以便告知浏览器始终向服务器询问页面的最新版本,因此用户必须重新登录。

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

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