I am using Visual Studio 2017, and I have an asp.NET Core MVC project where some users have multiple Roles. I am wondering if there is a way to set just one of these Roles to be the user's only Role for the remainder that user's session.
For example, if the user belongs to two Roles, NormalUser and AdminUser, and they want to use the app as a NormalUser, is there something I can set so that a Role check like User.IsInRole(nameof(RoleNames.AdminUser))
will return false, but User.IsInRole(nameof(RoleNames.NormalUser))
will return true?
Does the framework provide any such functionality?
No. If a user is in a role, they are in that role. The main problem here, I think, is that you're trying to use roles for a purpose they are not meant for. If you want an "admin user" to be able to see the site as a "normal user", you could simply add something to the session:
Session["ViewAsNormalUser"] = true;
Then, you can simply do your checks like:
if (!User.IsInRole(nameof(RoleNames.AdminUser)) || Session["ViewAsNormalUser"] as bool? == true)
{
// do normal user thing
}