下次通过检查记住密码记住我在mvc 4登录时的情况

时间:2014-05-12 11:27:15

标签: asp.net-mvc-4 authorization remember-me httpcookie

我想在下次登录页面

中选中“记住我”复选框时记住登录密码

我有Set AuthCookie的这种方法

private void SetAuthCookie(string userName, string roleofUser, bool presistantCookie)
        {
            double timeout = presistantCookie ? FormsAuthentication.Timeout.TotalMinutes : 30;

            DateTime now = DateTime.UtcNow.ToLocalTime();
            TimeSpan expirationTimeSapne = TimeSpan.FromMinutes(timeout);

            var authTicket = new FormsAuthenticationTicket(
                1,
                userName,
                now,
                now.Add(expirationTimeSapne),
                presistantCookie,
                roleofUser,
                FormsAuthentication.FormsCookiePath
                );

            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

            var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Path = FormsAuthentication.FormsCookiePath
            };

            if (FormsAuthentication.CookieDomain != null)
            {
                authCookie.Domain = FormsAuthentication.CookieDomain;
            }

            if (presistantCookie)
                authCookie.Expires = DateTime.Now.AddMinutes(timeout);
            Response.Cookies.Add(authCookie);

        }

我使用mvc 4

谢谢你的帮助

0 个答案:

没有答案
相关问题