asp.net – 运行时MVC更新FormsAuthenticationTicket UserData
我正在编写一个带有自定义身份验证和授权的MVC 4 Web应用程序.当用户登录该站点时,我创建一个FormsAuthenticationTicket并将其存储在cookie中
public void SignIn(string userName,bool createPersistentCookie,string UserData) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.","userName"); // Create and tuck away the cookie FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,userName,DateTime.Now,DateTime.Now.AddDays(15),createPersistentCookie,UserData); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(authTicket); //// Create the cookie. HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encTicket); HttpContext.Current.Response.Cookies.Add(faCookie); } UserData字符串将是一个以管道分隔的字符串,它将始终包含至少两个项目UserID | UserRole的.可以将用户分配给一个或多个角色,因此,UserData可能看起来像此UserID | UserRole | UserRole | UserRole的 然后我在Global.asax中拥有自己的自定义通用主体 protected void Application_AuthenticateRequest(Object sender,EventArgs e) { // Get the authentication cookie string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie = Context.Request.Cookies[cookieName]; // If the cookie can't be found,don't issue the ticket if (authCookie == null) return; // Get the authentication ticket and rebuild the principal // & identity FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); string[] UserData = authTicket.UserData.Split(new Char[] { '|' }); GenericIdentity userIdentity = new GenericIdentity(authTicket.Name); GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity,UserData); Context.User = userPrincipal; } 这一切都很好,但是,在我的应用程序中,如果用户有多个角色,当他们登录时,我需要列出他们的角色,然后让他们只选择一个角色去执行基于所选角色的功能. 我想,要做到这一点,也许我可以将用户选择的角色传递给方法,获取他们的FormsAuthenticationTicket并更新UserData以反映他们选择的角色.例如,使用1 | Manager | Applicant创建UserData字符串,然后我需要列出这两个角色并询问用户要在哪个角色下执行功能,他们选择Manager然后我将其FormsAuthenticationTicket中的UserData更新为1 |经理. 这甚至可能,或者有更好的方法吗? 任何帮助将不胜感激. 感谢大家. 解决方法
您可以随时更改FormsAuthenticationTicket.
HttpCookie cookie = FormsAuthentication.GetAuthCookie(Username,true); var ticket = FormsAuthentication.Decrypt(cookie.Value); var newticket = new FormsAuthenticationTicket(ticket.Version,ticket.Name,ticket.IssueDate,ticket.Expiration,true,"new user data",ticket.CookiePath); cookie.Value = FormsAuthentication.Encrypt(newticket); cookie.Expires = newticket.Expiration.AddHours(24); HttpContext.Current.Response.Cookies.Set(cookie); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – ViewState支持的属性不适用于列表
- asp.net – ASP .NET AJAX和JQuery
- ASP.NET – 图像未显示
- asp.net-mvc – Visual Studio断点未被击中
- asp.net-mvc – MVC3验证 – 需要一个组
- asp.net – 处理程序“PageHandlerFactory-ISAPI-4.0_32bit
- ASP.NET:创建Linked ValidationSummary, 深入理解ASP.NET的
- c# HttpClient 发送请求出错时解决办法 c# – 因
- asp.net – RDLC和SSRS之间的区别
- asp.net-core – Stream的意外结束,内容可能已被另一个组件
- asp.net-mvc – 在MVC应用程序中使用ELMAH调试/跟
- ASP.NET的临时文件下载链接
- asp.net – 如何编辑web.config以停止提供.NET W
- asp.net-mvc – 具有IE条件注释的MVC4捆绑/缩小
- asp.net-mvc – 在Visual Studio中使用TypeScrip
- asp.net – WebAPI不返回XML,即使有正确的Accept
- asp.net – 为什么我应该使用N层方法当使用SqlDa
- asp.net – 可以在web.config中添加响应http头吗
- asp.net – 在asp页面中显示来自其他站点的网页
- Pro ASP.NET Core MVC 2翻译计划:第一章