4

我将 FormsAuthentication 用于 ASP.NET 站点,该站点具有显示当前登录用户 Page.User.Identity.Name 的母版页。

他们可以在他们的设置中更改他们的用户名,当这样做时,我会为他们更新他们的 cookie,这样他们就不必通过回发退出/重新登录。

FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(username, false);

我可能很挑剔,但是在他们更改用户名后,母版页仍会显示其原始用户名,直到他们重新加载或加载不同的页面。

有没有办法以编程方式更新当前的 Page.User,以便在同一个回发期间显示他们的新用户名?

4

2 回答 2

8

虽然 MasterMax 的建议是我会做的,但您实际上可以更新Page.Uservia HttpContext.Current.User

如果您知道用户的角色(或者您没有使用基于角色的授权),则可以利用System.Security.Principal.GenericPrincipal该类:

string newUsername = "New Username";
string[] roles = new string[] {"Role1", "Role2"};

HttpContext.Current.User = 
   new GenericPrincipal(new GenericIdentity(newUserName), roles);
于 2009-03-17T14:56:26.913 回答
1

您可以创建母版页类的实例,并将您为用户名设置的属性设为公开,以便您可以在 FormsAuthentication 代码之后立即设置该属性。

于 2009-03-02T20:30:18.067 回答