Asp.net MVC 身份验证勾选而不创建身份验证 cookie
我正在使用“FormsAuthentication.SetAuthCookie”方法对应用程序中的用户进行身份验证,但是当我关闭浏览器并重新打开它时,它仍然经过身份验证,但会话已经结束,然后我的应用程序崩溃,因为它有必要的数据在会话上生成菜单。
我想要做的是:创建一个身份验证票证而不创建身份验证 cookie,每当用户在新的浏览器会话中打开页面时,它都会再次请求登录。
我怎样才能做到这一点。
I'm authenticating a user in my application with the method "FormsAuthentication.SetAuthCookie" method, but when I close the browser and reopen it, it is still authenticated, but the session is over already, then my app crashes because it has necessary data on the session to generate the menus.
What I want to do is the following: Create an authentication ticket without create a auth cookie to, whenever the user open the page in a new browser session it will request the login once again.
How can I achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会话和身份验证是不同的。如果您希望 cookie 在浏览器关闭时消失,请使用
Sessions and authentication are different. If you want the cookie to go away when the browser closes then use
是否有其他浏览器窗口打开?有时,如果有其他浏览器窗口打开,cookie 将持续存在。
Are there other browser windows open? Sometime if there are other browser windows open the cookie will persist.
Cookie 或会话上没有任何属性可以在浏览器关闭时使它们过期。它们仅受过期时间的控制(任何指定 cookie 标准的人都应该考虑到这一点)。
将 false 传递给 SetAuthMethod 不会告诉浏览器在浏览器关闭时删除 cookie。事实上,如果浏览器启用了返回到打开的选项卡的选项,它将重新打开这些选项卡以及其中的所有 cookie。如果这些身份验证 cookie 尚未过期,您将发现自己已登录。
您可以做的是,为用户选中“记住我”和不选中它时指定不同的过期时间:
There is no properties on the either cookies or session to expire them when the browser is closed. They are only controlled by expiration time (Something that should have been thought of by whoever specified standards for cookies).
Passing false to SetAuthMethod will not tell browser to delete cookie when the browser is closed. In fact, if browser has an option enabled to return to the tabs that were left open, it will reopen these tabs with all cookies that were there. If these authentication cookies did not expire yet, you will find your self logged in.
What you can do, is specify different expiration time for when user checks "Remember Me" vs when he does not check it: