简体   繁体   English

如何检查当前用户在 ASP.NET MVC 5 中的角色

[英]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.登录后,MVC 部分视图会为用户显示导航选项。 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.所以很明显,在新的 MVC 中,他们默认禁用了角色管理器,并有一种新的方式来做到这一点。 No biggie.没什么大不了的。 Searching the issue suggested that I enable the feature in web.config.搜索问题建议我在 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.我遵循了一些关于如何做到这一点的说明(我保证我可以谷歌搜索),但它似乎与我的 SQL Server 连接字符串混乱,给我的错误表明它正在尝试登录到一个不存在的本地数据库而不是我的 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?无论如何,长话短说,与其解决并重新启用残留的身份功能,不如在新的 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.同样,充满角色的 Viewbag 也无法工作,因为这是在每个页面上进行导航。

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.这是有道理的,因为 MVC 正在从基于角色的对象转向基于用户的对象。

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.最好询问一次并将其保存在 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.您可以根据需要多次控制用户的角色,而无需发出其他请求,而且更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取ASP.NET Core MVC中当前登录用户的角色 - Get role/s of current logged in user in ASP.NET Core MVC 基于当前用户的“角色”动态构建ASP.NET MVC主页面菜单 - Building an ASP.NET MVC Master Page Menu Dynamically, Based on the current User's “Role” ASP.NET Identity 2.0检查当前用户是否处于角色IsInRole中 - ASP.NET Identity 2.0 check if current user is in role IsInRole ASP.Net MVC 5 检查 _Layout 的用户角色 - ASP.Net MVC 5 Check User Role For _Layout 如何在执行ASP.NET MVC4中的方法之前检查用户权限角色访问 - How to check user permission Role Access before executing a method in ASP.NET MVC4 如何刷新当前经过身份验证的用户的角色更改了ASP.NET身份框架中的成员身份? - How to refresh current authenticated user's role changed membership in ASP.NET identity framework? mvc 5 asp.net身份角色和用户,如何避免同时使用户具有其角色并删除 - mvc 5 asp.net identity role and users, how to lest both in order user with it's roles and remove ASP.NET MVC - 如何根据登录用户的角色权限隐藏或显示链接/按钮? - ASP.NET MVC - How to hide or Show a link/button based on logged in User's Role permission? 如何在NTLM ASP.net MVC 5中向用户添加角色 - how add role to user in NTLM ASP.net MVC 5 如何检查用户是否在 Asp.Net Core Identity Framework 中担任角色 - How to check if a user is in a role in Asp.Net Core Identity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM