小编Mic*_*own的帖子

保持用户登录 - FormsAuthentication

我正在努力解决这个问题.我正在使用FormAuthentication.当用户登录并且他们检查记住我时,我希望用户保持登录24小时.问题是,无论我做什么,用户都会在30分钟后自动注销.我们用户选择了记住我,我设置了一个持久性cookie,24小时后到期.我可以在浏览器选项中看到cookie,并且过期是正确的.如果我离开网站并回去说一个小时.用户已注销......他是我所拥有的一些代码片段.

bool IsValid = Membership.ValidateUser(LoginControl.UserName, LoginControl.Password);

if (IsValid)
{
    e.Authenticated = true;

    if (LoginControl.RememberMeSet)
    {
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(LoginControl.UserName, true, 1440); // 1 day
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        cookie.Expires = authTicket.Expiration;
        HttpContext.Current.Response.Cookies.Set(cookie);
        Response.Redirect(FormsAuthentication.GetRedirectUrl(LoginControl.UserName, true), true);
        FormsAuthentication.SetAuthCookie(LoginControl.UserName, true);
        FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, true);
    }
    else
    {
        FormsAuthentication.SetAuthCookie(LoginControl.UserName, false);
        FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的web.config

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" defaultUrl="/" timeout="1" cookieless="UseCookies"  protection="All" slidingExpiration="true" ticketCompatibilityMode="Framework40"/>
</authentication>
Run Code Online (Sandbox Code Playgroud)

当用户没有检查记住我时,我设置了一个非持久性cookie,并且在1分钟不活动后用户注销.这是正常的.问题是当设置了记住cookie并且用户返回时,即使存在cookie,用户也不再登录.

asp.net forms-authentication

5
推荐指数
1
解决办法
7781
查看次数

标签 统计

asp.net ×1

forms-authentication ×1