First of all, I know about this question: Does MVC 5 AddToRole require logging out before it works?
and this one: What is the IUserSecurityStampStore <TUser> interface?
therefore, please do not mark this as a duplicate.
I am trying to add another user to a role (i.e. the user we are adding to this role is not the current user. If so, the answer to the first question I'm attached to is sufficient.)
Same:
IdentityResult result = await userManager.AddToRoleAsync(userID, roleName);
Two situations in which I do this are: from the admin page, where the current user is the administrator; and a webhook provided with basic authentication (where there is no current user at all).
PROBLEM: if the user this change belongs to logs in and uses the application, I need the "add to role" changes to be applied instantly. They will not need to log out again and again for this to happen, and this should happen right away.
Thanks to everyone.
EDIT: By the way, User.IsInRole (roleName) requires logging out and logging in to reflect the addition to the new role. UserManager.IsInRole (userID, roleName) does not do this because (I assume) it goes directly to the database tables that need to be checked. But if the user clicks on an action method protected by the role to which they have just been added, they will still have to log in again, which is fair enough. Still curious if there is a way around this.
EDIT: Here is the source code of the Authorize attribute: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/4e40cdef9c8a8226685f95ef03b746bc8322aa92/src/System.Web.Mvc/AuthorizeAttribute.cs
It uses User.IsInRole, so we have to log in again. It seems like the override method is AuthorizeCore (HttpContextBase httpContext). I'm not brave and not good enough to end this right now, but if you want a lot of people to find it useful.
nmit026
source share