关闭浏览器后登录状态丢失

The*_*per 1 c# asp.net

我有一个使用FormsAuthentication的登录系统,由于某种原因,在关闭和打开浏览器时会将我注销.以下是我设置登录代码的方法:

            FormsAuthenticationTicket ticket;

                ticket = new FormsAuthenticationTicket(1, tbUsername.Text, DateTime.Now, DateTime.Now.AddYears(1), true, string.Empty, FormsAuthentication.FormsCookiePath);

            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;

            //Add the cookie to the request
            Context.Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我已将cookie设置为跨会话持久化.这是我的web.config部分:

<authentication mode="Forms">
  <forms slidingExpiration="false" loginUrl="~/Login.aspx" name="BOIGAUTH" defaultUrl="~/Admin/Settings.aspx"/>
</authentication>
Run Code Online (Sandbox Code Playgroud)

此外,在此特定应用程序上禁用SessionState.谁知道什么是错的?

顺便离开应用程序浏览器超过2个小时让我登录,即使我没有与网站互动.关闭浏览器时,cookie才会丢失.

Ale*_*kov 5

如果您没有在cookie上设置过期,它将是仅限浏览器会话的cookie,并在您关闭所有浏览器实例后消失.

您需要在cookie上设置一些过期,以使其与Expires属性一致.

HttpCookie cookie = new HttpCookie(
   FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
cookie.Expires = DateTime.Now.AddMinutes(1000);
Run Code Online (Sandbox Code Playgroud)

附注:确保您的浏览器未配置为关闭时清除所有Cookie.

  • 我是说票,对不起。它有一个过期值,所以我认为它是 cookie 过期。将尝试在 cookie 和票证上设置它。 (2认同)