This tutorial
Create an ASP.NET Core app with user data protected by authorization on learn.microsoft.com
Teaches us to use Roles to handle authorization.
However, with regard to Roles, there is one thing I noticed:
If a user is added to a Role while they are logged in, the changes don't seem to be visible until they log out.
That is: If I call this while this particular identityUser
is logged in:
userManager.AddToRoleAsync(idenityUser, role)
Then the operation shown in the tutorial to check if the current user is in a role:
context.User.IsInRole(role)
Returns FALSE even if User
refers to this particular identityUser
. And it keeps returning false until the user logs out and in again.
I actually tried to enhance the app in the tutorial to add a Promote functionality, that is, allow admins to promote regular users to Managers. And yes, the user being promoted had to log out and in to eventually become a Manager after he was promoted.
Now requireing users to do this when they are being promoted seems to be an unecessary inconvenience. But for issues like laying down banhammer this is, obviously, unacceptable. The banned user likely won't be kind enough to log out promptly.
Is there any way to force adding a user to a Role to be effective immediately, even if the said user is logged in at the moment?