如标题User.IsInRole()
在 Controller 中总是返回 false,但在 View 中效果良好。另外我无法设置
<roleManager enabled="true" />
,因为我在项目构建时遇到以下错误
It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.
有什么建议吗?或者还有其他方法来检查当前用户是否处于角色中?
(我尝试了 Roles.IsUserInRole(User.Identity.Name, "Recruit")
但我得到了相同的结果)。我正在使用 ASP.NET MVC 5 和 .NET Identity 6.2
最佳答案
Roles.IsUserInRole
是 MembershipProvider 的一部分。如果您使用的是 Asp.Net Identity,则需要使用 RoleManager
( MSDN )。
原因是您可以使用User.IsInRole
- 身份框架支持此调用,它会查找 cookie,检查 cookie 中可用的角色。
关于c# - User.IsInRole() 仅在 Controller 中始终返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600530/