Asp.Net的mvc不得注销(Asp.Net Mvc Can Not Log Out)

2019-07-30 09:58发布

这里是我的代码登录

 var expire = DateTime.Now.AddDays(7);
        // Create a new ticket used for authentication
        var ticket = new FormsAuthenticationTicket(
        1, // Ticket version
        username, // Username to be associated with this ticket
        DateTime.Now, // Date/time issued
        expire, // Date/time to expire
        true, // "true" for a persistent user cookie (could be a checkbox on form)
        roles, // User-data (the roles from this user record in our database)
        FormsAuthentication.FormsCookiePath); // Path cookie is valid for

        // Hash the cookie for transport over the wire
        var hash = FormsAuthentication.Encrypt(ticket);
        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash) { Expires = expire };

        // Add the cookie to the list for outbound response
        Response.Cookies.Add(cookie);

这是我的代码来检查的作用。 这是一个自定义IHTTP模块

 if (HttpContext.Current.User == null) return;
        if (!HttpContext.Current.User.Identity.IsAuthenticated) return;
        if (!(HttpContext.Current.User.Identity is FormsIdentity)) return;

        // Get Forms Identity From Current User
        var id = (FormsIdentity)HttpContext.Current.User.Identity;
        // Get Forms Ticket From Identity object
        var ticket = id.Ticket;
        // Retrieve stored user-data (our roles from db)
        var userData = ticket.UserData;
        var roles = userData.Split(',');
        // Create a new Generic Principal Instance and assign to Current User
        Thread.CurrentPrincipal = HttpContext.Current.User = new GenericPrincipal(id, roles);

这里是我的代码来注销

FormsAuthentication.SignOut();
        Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
        Session.Clear(); 
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache");
        return View("SignIn");

这太疯狂了。 我有两个秃斑现在。

Answer 1:

1)不应该您的来电Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 是Response.Cookies.Remove(无论-的用户名-IS);?

2)尝试发送一个过期的cookie返回给浏览器。

FormsAuthentication.SignOut();

// replace with username if this is the wrong cookie name
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Session.Clear(); 
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");

// send an expired cookie back to the browser
var ticketExpiration    = DateTime.Now.AddDays(-7);
var ticket              = new FormsAuthenticationTicket(
    1, 
    // replace with username if this is the wrong cookie name
    FormsAuthentication.FormsCookieName, 
    DateTime.Now, 
    ticketExpiration, 
    false, 
    String.Empty);
var cookie              = new System.Web.HttpCookie("user")
{
    Expires             = ticketExpiration,
    Value               = FormsAuthentication.Encrypt(ticket),
    HttpOnly            = true
};

Response.Cookies.Add(cookie);

return View("SignIn");


Answer 2:

您不能直接删除客户端的计算机上的cookie。 当你调用Cookies.Remove方法的cookie被在服务器端删除。 要删除一个客户机端的cookie有必要对Cookie的到期日期设置为过去的日期。

HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
     cookie.Expires = DateTime.Now.AddDays(-1);
     HttpContext.Current.Response.Cookies.Add(cookie);
}

我希望这可以帮助你。



Answer 3:

如果你想“在浏览器中没有缓存回”应用行为的所有网页上,那么你应该把它放在Global.asax中。

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}

希望它可以帮助别人!



文章来源: Asp.Net Mvc Can Not Log Out