User.IsInRole doesn't work

后端 未结 16 1405
北恋
北恋 2020-12-05 18:29

I have ASP.NET MVC 4 application. I use Simple Membership Provider allowing to tick remember me checkbox under login form. If ticked, persitent cookie .ASPXAUTH is created w

16条回答
  •  臣服心动
    2020-12-05 18:47

    I've been struggling with the MVC5 role manager for some time now. I've verified that User.IsInRole can return different results at the same time, and apparently this is because the User object is cached and needs to be invalidated. One way to do this is to log out and log back in again as one of the answers here specified. Just throw this in the controller that adds the new role:

            UserManager.AddToRole(User.Identity.GetUserId(), "Administrator");
    
            var updatedUser = await UserManager.FindByNameAsync(User.Identity.Name);
            var newIdentity = await updatedUser.GenerateUserIdentityAsync(UserManager);
            AuthenticationManager.SignOut();
            AuthenticationManager.SignIn(newIdentity);
    

    However, if the user has your application open in other windows this won't update all of them.

提交回复
热议问题