Programmatically Update / Update HttpContext.User

I am using FormsAuthentication for an ASP.NET site with a main page that displays the currently logged-in user, Page.User.Identity.Name.

They can change their username in their settings, and when this happens, I update their cookie for them, so they should not log out / leave messages with a postback.

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

I'm probably pretty picky, but after they changed their username, the main page still displays its original username until it reloads or another page loads.

Is there a way to programmatically update the current Page.User so that their new username is displayed during the same postback?

+4
source share
2 answers

Although the MasterMax proposal is what I would do, you can actually update Page.User through HttpContext.Current.User .

If you know user roles (or you do not use role-based authorization), you can use 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); 
+6
source

you can create an instance of the master page class and make the property that you configure for the username so that you can set this property right after the FormsAuthentication code.

+1
source

All Articles