Programmatically refresh/update HttpContext.User

前端 未结 2 558
夕颜
夕颜 2021-01-13 13:39

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 us

相关标签:
2条回答
  • 2021-01-13 14:23

    Though MasterMax's suggestion is what I would do, you can actually update the Page.User via HttpContext.Current.User.

    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);
    
    0 讨论(0)
  • 2021-01-13 14:35

    you could create an instance of your master page class, and make the property that you're setting for the username public, so that you can set that property right after your FormsAuthentication code.

    0 讨论(0)
提交回复
热议问题