asp.net mvc表单身份验证丢失cookie会话

时间:2017-12-18 16:59:20

标签: asp.net-mvc forms authentication model-view-controller

asp.net mvc项目发布时,它会在15-20分钟后丢失会话。

我的global.asax

enter code  protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());

        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);


    }

    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
        var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            if (authTicket != null && !authTicket.Expired)
            {
                var roles = authTicket.UserData.Split(',');
                HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
            }
        }
    }

登录方式:

   //clear any other tickets that are already in the response
                    Response.Cookies.Clear();

                    //set the new expiry date - to thirty days from now
                    DateTime expiryDate = DateTime.Now.AddDays(30);

                    //create a new forms auth ticket
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, login.EmailID, DateTime.Now, expiryDate, true, "Kullanici Bilgisi");


                    //encrypt the ticket
                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);


                    //create a new authentication cookie - and set its expiration date
                    HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    authenticationCookie.Expires = ticket.Expiration;
                    authenticationCookie.HttpOnly = true;

                    //add the cookie to the response.
                    Response.Cookies.Add(authenticationCookie);

请帮帮我。我控制谷歌开发者工具。我看到超时2018,但会话总是丢失重定向到登录页面。

Web配置:

 <authentication mode="Forms">
  <forms cookieless="UseCookies" loginUrl="~/user/login" slidingExpiration="true"></forms>

</authentication>

0 个答案:

没有答案