MVC5/EF6角色管理器

MVC5/EF6 RoleManager

在我的 Index.cshtml 中,我正在检查 UserRole

@if (Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Standard"))
{
}

当我在浏览器中加载 Index.cshtml 时,收到此错误:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: Unable to connect to SQL Server database.

我已经阅读了多个不适合我的解决方案:

所有这些事情对我来说都没有成功。我真的很想在MVC5/EF6中使用authentication/authorization。我想要 MVC5/EF6/Identity 的说明,因为大多数人会回答 MemberShip 或其他问题。

您正在使用的 Roles class 是旧会员时代的遗留物,一个身份验证框架,现在已被 ASP.NET Identity 取代。要执行角色检查,您必须通过 User 属性 执行此操作,这是一个由 OWIN 身份验证中间件填充的 IPrincipal。您的代码最终将如下所示:

@if (User.IsInRole("Admin") || User.IsInRole("Standard"))
{
    ....
}