如何设置formauthentication过期时间?它不起作用,10分钟后到期

时间:2012-09-09 21:08:40

标签: asp.net-mvc-3

您使用表单身份验证并创建故障单并将其添加到Response它运行良好。

当我查看Firefox工具创建的Cookie> pageinfo> security> cookies时,会发现当前设置在Cookie上的过期时间。

它在本地运行良好,但当我在服务器(服务器2008-iis7)上传它时,到期时间 不起作用,但设置在cookie上,总是我的cookie到期约10减去成员将 登出。 有一些特殊的设置,我在互联网上看一些例子,但无法找到任何事情。

我的验证码:

 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1,
    username,//username
    DateTime.Now,
    DateTime.Now.AddMinutes(120),
    true,
    rollname,
    FormsAuthentication.FormsCookiePath); 

    string hashCookies = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashCookies); // Hashed ticket
    cookie.Expires = DateTime.Now.AddMinutes(120);

      Response.Cookies.Add(cookie);

我在global.asax中使用此方法来检查请求身份验证:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        // look if any security information exists for this request
        if (System.Web.HttpContext.Current.User != null)
        {

         // see if this user is authenticated, any authenticated cookie (ticket) exists for this user
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {

                // see if the authentication is done using FormsAuthentication
                if (System.Web.HttpContext.Current.User.Identity is FormsIdentity)
                {

                   // Get the roles stored for this request from the ticket
                    // get the identity of the user
                    FormsIdentity identity = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;

                   // get the forms authetication ticket of the user
                    FormsAuthenticationTicket ticket = identity.Ticket;

                   // get the roles stored as UserData into the ticket 
                    string[] roles = ticket.UserData.Split(',');

                    // create generic principal and assign it to the current request
                    System.Web.HttpContext.Current.User = new  System.Security.Principal.GenericPrincipal(identity, roles);
                }
            }
        }
    }

在我的网络配置中:

<authentication mode="Forms">

  <forms loginUrl="~/Home.aspx" timeout="120" />

</authentication>

0 个答案:

没有答案