简体   繁体   中英

How to check current user's role in ASP.NET MVC 5

currently dealing with an interesting problem. I am building a website with three different user roles. When logged in, the MVC partial view shows navigation options for the users. I want to show different options depending on the user's role. In previous websites, I have used the following code to determine a role:

@if (Roles.IsUserInRole("intern"))
{
    <li>@Html.ActionLink("Log Time", "Index", "Time")</li>
}

Unfortunately, when I attempted this in my current code, I got the message:

The Role Manager feature has not been enabled.

So apparently in the new MVC they disable the role manager by default and have a new way of doing it. No biggie. Searching the issue suggested that I enable the feature in web.config. I followed several instructions on how to do that (I promise I can google search) but it seems to mess with my SQL Server connection string, giving me errors that indicate it's trying to log in to a local db that doesn't exist rather than my Azure SQL Server. I've played around for a while and I don't know why this is the case.

Anyway, long story short, rather than work around and re-enable a vestigial Identity feature, how are you supposed to accomplish this in the new MVC? I can get the roles fine controller side with user manager, but I can't use that in a view. Similarly a Viewbag full of roles can't work because this is navigation on every page.

I appreciate all the help in advance, thanks everyone!

Just found the answer, I'll leave this up for other people dealing with this. The correct way to do this is:

@if (User.IsInRole("intern"))

This makes sense since MVC is moving away from Role based objects and towards User based objects.

I think it's not really a good idea to ask for the user's role all the time, too many requests are made. It would be better to ask once and save it on a variable in Razor. Then just check that variable whenever you need it. By the way, if the roles are different, you don't even want to ask if the user is in that role rather than another one. Rather get the list of roles in a list and check if the role indicated is in the list.

Example (I'm not sure it will compile, look a the idea):

@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.Localization
@inject UserManager<ApplicationUser> UserManager
@{
    ApplicationUser currentUser = await UserManager.GetUserAsync(User);
    var roles = await UserManager.GetRolesAsync(currentUser);
    bool isIntern = roles.Contains("intern");
    bool isExtern = roles.Contains("extern");
    bool isFoo = roles.Contains("foo");
    ...
} 

then, further on

@if (isIntern)
{
     <li>@Html.ActionLink("Log Time", "Index", "Time")</li>
}
else if (isExtern)
{
     ...
}

You can control the role of the user as many times as you want without having to make other requests and it's all much more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM