Asp.net MVC 身份验证勾选而不创建身份验证 cookie

发布于 2024-08-05 16:36:51 字数 220 浏览 3 评论 0原文

我正在使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独患者 2024-08-12 16:36:51

会话和身份验证是不同的。如果您希望 cookie 在浏览器关闭时消失,请使用

FormsAuthentication.SetAuthCookie("username", false);

Sessions and authentication are different. If you want the cookie to go away when the browser closes then use

FormsAuthentication.SetAuthCookie("username", false);
云淡月浅 2024-08-12 16:36:51

是否有其他浏览器窗口打开?有时,如果有其他浏览器窗口打开,cookie 将持续存在。

Are there other browser windows open? Sometime if there are other browser windows open the cookie will persist.

若有似无的小暗淡 2024-08-12 16:36:51

Cookie 或会话上没有任何属性可以在浏览器关闭时使它们过期。它们仅受过期时间的控制(任何指定 cookie 标准的人都应该考虑到这一点)。

将 false 传递给 SetAuthMethod 不会告诉浏览器在浏览器关闭时删除 cookie。事实上,如果浏览器启用了返回到打开的选项卡的选项,它将重新打开这些选项卡以及其中的所有 cookie。如果这些身份验证 cookie 尚未过期,您将发现自己已登录。

您可以做的是,为用户选中“记住我”和不选中它时指定不同的过期时间:

// create this private method.
private void SetAuthCookie(string emailAddress, bool keepMeLoggedIn)
{
    int timeout = keepMeLoggedIn ? 525600 : 20; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(emailAddress, keepMeLoggedIn, timeout);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
    cookie.HttpOnly = true; // cookie not available in JavaScript.
    HttpContext.Response.Cookies.Add(cookie);
}

// use it instead of FormsAuthentication.SetAuthCookie(model.username, model.RememberMe);
SetAuthCookie(model.username, model.RememberMe);

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:

// create this private method.
private void SetAuthCookie(string emailAddress, bool keepMeLoggedIn)
{
    int timeout = keepMeLoggedIn ? 525600 : 20; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(emailAddress, keepMeLoggedIn, timeout);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
    cookie.HttpOnly = true; // cookie not available in JavaScript.
    HttpContext.Response.Cookies.Add(cookie);
}

// use it instead of FormsAuthentication.SetAuthCookie(model.username, model.RememberMe);
SetAuthCookie(model.username, model.RememberMe);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文