Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 以编程方式刷新/更新HttpContext.User_Asp.net_Forms Authentication_Httpcontext - Fatal编程技术网

Asp.net 以编程方式刷新/更新HttpContext.User

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); 我可能很挑剔,但在他们更改用户名后,母版页仍然显示他们的原始

我正在对ASP.NET站点使用FormsAuthentication,该站点的母版页显示当前登录的用户page.user.Identity.Name

他们可以在自己的设置中更改用户名,当用户更改用户名时,我会为他们更新cookie,这样他们就不必使用回发来注销/重新登录

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);