角色管理器功能尚未启用。

198

收到以下ProviderException

角色管理器功能未启用。

目前为止还不错。

是否有某个方法可以调用来检查角色管理器是否已启用?

7个回答

306

您可以通过读取以下布尔属性来实现:

System.Web.Security.Roles.Enabled

这是从web.config文件的roleManager元素的enabled属性中直接读取的结果:
<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>


更新:
更多信息请查看此MSDN示例:https://msdn.microsoft.com/zh-cn/library/aa354509(v=vs.110).aspx


1
我该如何在代码中实现这个而不是在web.config中?我尝试将其放在Application_Start中,但它显示“此方法只能在应用程序的预启动初始化阶段调用”。 - Maslow
1
这个应该放在 web.config 文件的哪里? - Matt Connolly
23
在web.config文件中添加上述内容后,roleManager被启用。但现在我遇到了“无法连接到SQL Server数据库”的异常。 - Irfan Y
2
@Infotekka "无法连接到 SQL Server 数据库" 错误。 - Jack
2
哇,这是一个很棒的答案,我甚至不需要配置任何东西,它就像魔法一样正常工作。一旦在web.config中配置好了,我只需检查User.Identity.IsAuthenticated即可查看登录用户是否已通过身份验证。ASP.NET太酷了。 - Quan
显示剩余3条评论

53

如果您来到这里是因为您正在使用新的ASP.NETIdentityUserManager,实际上您要找的是RoleManager

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

roleManager 将让您可以访问到角色是否存在、创建等信息,此外它还是为了UserManager而创建的。


74
三岁的孩子与任何事情有什么关系?我是因为设置身份验证出了问题,从谷歌点击进入这篇帖子。既然我已经解决了这个问题,下一个遇到同样问题并通过谷歌跳转到此处的人将会知道该怎么做。请注意,不要改变原文意思,并尽可能地使翻译通俗易懂。 - Serj Sagan
2
此外,Identity UserManager 还有一个有用的功能,可以获取给定用户的角色:UserManager.GetRolesAsync(User.Identity.GetUserId()); - keithl8041
你把 var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); 放哪里? - Mario
你可以在应用程序的任何地方使用它。显然,您需要解决一些引用问题,但是在身份应用程序中需要角色的任何地方,您都可以使用此语句。 - Serj Sagan

11

我在Google上找到了两个建议,建议A是确保您的数据库连接字符串(角色使用的连接字符串)正确,并且键名拼写正确;建议B是确保RoleManager的Enabled标志设置为true。希望其中一个可以帮到你,对我有用。

您试过检查Roles.Enabled吗?还可以检查Roles.Providers查看有多少提供程序可用,以及检查默认提供程序的Roles.Provider。如果它为null,则没有提供程序。


谢谢您的回答。但这不是我想要的。 我想要一个方法,检查角色管理器功能是否启用,而不需要为此缓存ProviderException。 - gsharp

8
如果您正在使用ASP.NET Identity UserManager,您也可以像这样获取它:
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();

var roles = userManager.GetRoles(User.Identity.GetUserId());

如果您已将用户密钥从GUID更改为整数,可以使用以下代码:

如果你已经把用户的key由Guid改成了Int,请使用以下代码:

var roles = userManager.GetRoles(User.Identity.GetUserId<int>());

这对我有用。如果您投票反对,请说明原因。 - Ogglas
这段代码无法正常工作,因为你需要将userid强制转换为int类型,如下所示: userManager.GetRoles(Convert.ToInt32(User.Identity.GetUserId<int>())); - toy
@toy 不需要转换,<int> 将会得到 int 类型的值。当然,为了使这个方法生效,Identity 键必须是 int 类型。 - Ogglas

7
我因为异常问题找到了这个问题。我的Web.Config没有任何<roleManager>标签。我意识到即使我添加了它(如Infotekka建议的),也会出现数据库异常。在遵循此处其他答案中的建议后,仍然无法完全解决该问题。
由于这些Web.Config标签可以自动生成,因此手动添加它们感觉不对。如果您遇到类似情况,请撤消对Web.Config和Visual Studio所做的所有更改:
  1. Press Ctrl+Q, type nuget and click on "Manage NuGet Packages";
  2. Press Ctrl+E, type providers and in the list it should show up "Microsoft ASP.NET Universal Providers Core Libraries" and "Microsoft ASP.NET Universal Providers for LocalDB" (both created by Microsoft);
  3. Click on the Install button in both of them and close the NuGet window;
  4. Check your Web.config and now you should have at least one <providers> tag inside Profile, Membership, SessionState tags and also inside the new RoleManager tag, like this:

    <roleManager defaultProvider="DefaultRoleProvider">
        <providers>
           <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=NUMBER" connectionStringName="DefaultConnection" applicationName="/" />
        </providers>
    </roleManager>
    
  5. Add enabled="true" like so:

    <roleManager defaultProvider="DefaultRoleProvider" enabled="true">
    
  6. Press F6 to Build and now it should be OK to proceed to a database update without having that exception:

    1. Press Ctrl+Q, type manager, click on "Package Manager Console";
    2. Type update-database -verbose and the Seed method will run just fine (if you haven't messed elsewhere) and create a few tables in your Database;
    3. Press Ctrl+W+L to open the Server Explorer and you should be able to check in Data Connections > DefaultConnection > Tables the Roles and UsersInRoles tables among the newly created tables!

2
“由于 Web.Config 字段是自动生成的” 这种说法并不完全正确。虽然许多 NuGet 包会自动调整配置文件,但并没有强制要求它们这样做的规定。 - Kevin R.
这很完美。谢谢。 - sapatelbaps

-1
<roleManager
  enabled="true"
  cacheRolesInCookie="false"
  cookieName=".ASPXROLES"
  cookieTimeout="30"
  cookiePath="/"
  cookieRequireSSL="false"
  cookieSlidingExpiration="true"
  cookieProtection="All"
  defaultProvider="AspNetSqlRoleProvider"
  createPersistentCookie="false"
  maxCachedResults="25">
  <providers>
    <clear />
    <add
       connectionStringName="MembershipConnection"
       applicationName="Mvc3"
       name="AspNetSqlRoleProvider"
       type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <add
       applicationName="Mvc3"
       name="AspNetWindowsTokenRoleProvider"
       type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</roleManager>

-1

以下是你需要在MVC5及更高版本的Account Controller中放置的代码,以获取用户角色列表:

csharp public async Task<ActionResult> RoleAdd(string UserID) { return View(await UserManager.GetRolesAsync(UserID)).OrderBy(s => s).ToList()); }

无需使用Roles.GetRolesForUser()并启用角色管理器功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接