表单身份验证/ Cookie即将过期

时间:2018-10-10 17:53:19

标签: asp.net authentication cookies forms-authentication

对于ASP.Net Web应用程序,cookie过期太早了。

我已将Cookie设置为3小时后过期,并且尚未在webconfig中设置超时,但是将用户在1小时后定向到登录屏幕。

如果我将Cookie的过期时间设置为1分钟,则它会在1分钟后将用户注销,因此我猜一个小时后其他原因将其覆盖,但是我不确定在哪里查看。

我的表单身份验证和会话状态Web配置条目以及用于创建cookie和查找cookie的代码如下所示。

<sessionState mode="InProc" timeout="525600" />
<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" name=".VRBAdmin" enableCrossAppRedirects="false" cookieless="UseCookies" />
</authentication>
<authorization>

protected void OnLogin(object sender, EventArgs e)
    {
        if (Membership.ValidateUser(this.uxUser.Text, this.uxPassword.Text))
        {
            string userData = string.Join("|", Roles.GetRolesForUser(this.uxUser.Text));

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,                                     // ticket version
            this.uxUser.Text,                              // authenticated username
            DateTime.Now,                          // issueDate
            DateTime.Now.AddHours(3),               // expiryDate
            true,                                  // true to persist across browser sessions
            userData,                                  // can be used to store additional user data
            FormsAuthentication.FormsCookiePath);  // the path for the cookie

            // Encrypt the ticket using the machine key
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            // Add the cookie to the request to save it
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            Response.Cookies.Add(cookie);

            // Your redirect logic
            Response.Redirect(FormsAuthentication.GetRedirectUrl(this.uxUser.Text, true));
        }
    }

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            //Extract the forms authentication cookie
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

            // If caching roles in userData field then extract
            string[] roles = authTicket.UserData.Split(new char[] { '|' });

            // Create the IIdentity instance
            IIdentity id = new FormsIdentity(authTicket);

            // Create the IPrinciple instance
            IPrincipal principal = new GenericPrincipal(id, roles);

            // Set the context user 
            Context.User = principal;
        }
    }

1 个答案:

答案 0 :(得分:0)

如果使用ASP.NET成员提供程序,则不应自己创建FormsAuthenticationTicket。您甚至不需要在principal事件内手动创建Application_AuthenticateRequest对象。

相反,您想让Membership Provider承担所有繁重的工作。

通常,会话超时应该比身份验证Cookie超时小两倍,因为我们需要释放资源。

<sessionState timeout="180" />
<authentication mode="Forms">
   <forms ...  timeout="360" />
</authentication>


protected void OnLogin(object sender, EventArgs e)
{
   if (Membership.ValidateUser(this.uxUser.Text, this.uxPassword.Text))
   {
       FormsAuthentication.SetAuthCookie(this.uxUser.Text, RememberMeSet);
       ...
   }
}

如果您的应用程序流量不足,也请increase application pool timeout