以编程方式刷新/更新HttpContext.User中 [英] Programmatically refresh/update HttpContext.User
问题描述
我使用FormsAuthentication对于具有显示当前登录的用户母版页的ASP.NET网站,Page.User.Identity.Name。
I'm using FormsAuthentication for an ASP.NET site that has a master page that displays the current logged in user, Page.User.Identity.Name.
他们可以改变他们的用户名在其设置中,当这样做,我更新自己的饼干他们,使他们不会有退出与回传/重新登录。
They can change their username in their settings, and when the do so, I update their cookie for them so they wont have to sign out/sign back in with a postback.
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(username, false);
我可能是pretty挑剔的,但他们改变自己的用户名后的母版页仍显示原来的用户名,直到他们重新加载或加载不同的页面。
I'm probably being pretty nit-picky, but after they change their username the master page still displays their original username until they reload or load a different page.
有没有办法以编程方式更新当前Page.User,这样可以在同一回发过程中显示他们的新用户名?
Is there any way to programmatically update the current Page.User, so that their new username can be displayed during the same postback?
推荐答案
虽然MasterMax的建议是我会做什么,你其实可以更新 Page.User
通过 HttpContext.Current.User
。
Though MasterMax's suggestion is what I would do, you can actually update the Page.User
via HttpContext.Current.User
.
如果您知道用户的角色(或不使用基于角色的授权),你可以利用 System.Security.Principal.GenericPrincipal
类:
If you know the user's roles (or you aren't using role based authorization), you can take advantage of the System.Security.Principal.GenericPrincipal
class:
string newUsername = "New Username";
string[] roles = new string[] {"Role1", "Role2"};
HttpContext.Current.User =
new GenericPrincipal(new GenericIdentity(newUserName), roles);
这篇关于以编程方式刷新/更新HttpContext.User中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!