Asp.net 以编程方式刷新/更新HttpContext.User
我正在对ASP.NET站点使用FormsAuthentication,该站点的母版页显示当前登录的用户page.user.Identity.Name 他们可以在自己的设置中更改用户名,当用户更改用户名时,我会为他们更新cookie,这样他们就不必使用回发来注销/重新登录Asp.net 以编程方式刷新/更新HttpContext.User,asp.net,forms-authentication,httpcontext,Asp.net,Forms Authentication,Httpcontext,我正在对ASP.NET站点使用FormsAuthentication,该站点的母版页显示当前登录的用户page.user.Identity.Name 他们可以在自己的设置中更改用户名,当用户更改用户名时,我会为他们更新cookie,这样他们就不必使用回发来注销/重新登录 FormsAuthentication.SignOut(); FormsAuthentication.SetAuthCookie(username, false); 我可能很挑剔,但在他们更改用户名后,母版页仍然显示他们的原始
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(username, false);
我可能很挑剔,但在他们更改用户名后,母版页仍然显示他们的原始用户名,直到他们重新加载或加载其他页面
是否有任何方法以编程方式更新当前页面.User,以便在同一回发过程中显示其新用户名?您可以创建母版页类的实例,并将为用户名设置的属性设置为公共,因此,您可以在FormsAuthentication代码之后设置该属性。虽然MasterMax的建议是我会做的,但您实际上可以通过
HttpContext.Current.User
更新页面.User
如果您知道用户的角色(或者您没有使用基于角色的授权),则可以利用System.Security.Principal.GenericPrincipal
类:
string newUsername = "New Username";
string[] roles = new string[] {"Role1", "Role2"};
HttpContext.Current.User =
new GenericPrincipal(new GenericIdentity(newUserName), roles);