asp.net-mvc – Asp.Net Mvc无法注销
发布时间:2020-12-16 07:07:44 所属栏目:asp.Net 来源:网络整理
导读:这是我的代码登录 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,// Da
这是我的代码登录
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"); 这太疯狂了.我现在有两个光头. 解决方法
1)不应该调用Response.Cookies.Remove(FormsAuthentication.FormsCookieName);是Response.Cookies.Remove(无论用户名是什么);?
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"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automap
- asp.net-mvc-5 – 我们也可以在Windows /桌面应用程序中使用
- asp.net – 这是一个很好的SOA架构吗?
- IIS上的ASP.NET Core 2.0错误502.5
- asp.net – CookieAuthenticationOptions.AuthenticationTy
- asp.net – 如何添加两个CSS类来控制代码背后?
- asp.net-mvc – ASP.NET MVC 2 DropDownList无法渲染
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-
- asp.net – Excel单元格对齐:例如,数值xlLeft,xlRight还是
- 用于ASP.NET的web api – 如何构建对象流
推荐文章
站长推荐
热点阅读