如何维护FormsAuthentication Cookie它不会在MVC中删除?

时间:2017-05-26 15:19:08

标签: c# asp.net-mvc authentication cookies forms-authentication

我遇到删除FormsAuthentication票证cookie的问题,记得我的选项..它总是显示过期日期01/01/0001,我在登录控制器时设置过期时间的FormsAuthentication票证。

当应用程序URL再次打开时,检查过期时间& Auth Cookie。

每次验证cookie过期时间显示相同..
如何管理FormsAuth票据并记住我的选项?

我在不同的论坛上搜索了很多但没有得到确切的解决方案..


在我没有选择记住我的情况下截图。 当我选择时记住我它在Expire age中显示时间。但不处理C#代码。 enter image description here

// Here u is view model object that set the username,password and remember me options.

//登录功能代码

if (u.RememberMe)
{
    int timeout = u.RememberMe ? 525600 : (int)sessionSection.Timeout.TotalMinutes; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(1, u.Email, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(timeout), u.RememberMe, logmodel.UserType);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Path = FormsAuthentication.FormsCookiePath;
    //cookie.Expires = System.DateTime.UtcNow.AddMinutes(timeout);// Not my line
    //if (ticket.IsPersistent)
    //    cookie.Expires = ticket.Expiration;
    cookie.Expires = ticket.Expiration;
    /// cookie.HttpOnly = true; // cookie not available in javascript.
    Response.Cookies.Add(cookie);
}
else
{
    FormsAuthentication.SetAuthCookie(u.Email, false);
}

0 个答案:

没有答案