User.IsInRole() 始终 returns false 仅在控制器中

User.IsInRole() always returns false only in controller

如标题 User.IsInRole() 控制器中始终 returns 错误,但在视图中运行良好。另外我不能设置 <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 身份,则需要使用 RoleManager (MSDN) 代替。

原因是您可以使用 User.IsInRole - 身份框架支持此调用,它查看 cookie,检查 cookie 中可用的角色。