角色管理器功能尚未启用

发布于 2024-09-27 00:22:19 字数 279 浏览 8 评论 0原文

得到以下 ProviderException:

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

到目前为止,一切都很好。

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

Got the following ProviderException :

The Role Manager feature has not been enabled.

So far so good.

Is there somewhere a method that can be called to check if the Role Manager has been enabled or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雨轻弹 2024-10-04 00:22:19

您可以通过读取布尔属性来执行此操作:

System.Web.Security.Roles.Enabled

这是从 web.configroleManager 元素的 enabled 属性直接读取>:

<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>

更新:
有关详细信息,请查看此 MSDN 示例:https: //msdn.microsoft.com/en-us/library/aa354509(v=vs.110).aspx

You can do this by reading from the boolean property at:

System.Web.Security.Roles.Enabled

This is a direct read from the enabled attribute of the roleManager element in the web.config:

<configuration>
  <system.web>
    <roleManager enabled="true" />
  </system.web>
</configuration>

Update:
For more information, check out this MSDN sample: https://msdn.microsoft.com/en-us/library/aa354509(v=vs.110).aspx

我做我的改变 2024-10-04 00:22:19

如果您来到这里是因为您正在使用新的 ASP.NET Identity UserManager,那么您实际上正在寻找的是 RoleManager

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

roleManager 将允许您查看角色是否存在、创建等,并且它是为 UserManager 创建的

If you got here because you're using the new ASP.NET Identity UserManager, what you're actually looking for is the RoleManager:

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

roleManager will give you access to see if the role exists, create, etc, plus it is created for the UserManager

你的他你的她 2024-10-04 00:22:19

我通过 Google 在其他地方找到了 2 个建议,建议 a) 确保您的数据库连接字符串(角色正在使用的连接字符串)正确且其密钥拼写正确,b) RoleManager 上的 Enabled 标志设置为 true。希望其中之一有所帮助。它对我有用。

您是否尝试检查 Roles.Enabled ?此外,您还可以检查 Roles.Providers 以查看有多少个可用的提供程序,并且可以检查 Roles.Provider 中的默认提供程序。如果它为空,则没有一个。

I found 2 suggestions elsewhere via Google that suggested a) making sure your db connectionstring (the one that Roles is using) is correct and that the key to it is spelled correctly, and b) that the Enabled flag on RoleManager is set to true. Hope one of those helps. It did for me.

Did you try checking Roles.Enabled? Also, you can check Roles.Providers to see how many providers are available and you can check the Roles.Provider for the default provider. If it is null then there isn't one.

苏别ゝ 2024-10-04 00:22:19

如果您使用的是 ASP.NET Identity UserManager,您也可以这样获取:

var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();

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

如果您已将用户的密钥从 Guid 更改为 Int,例如使用以下代码:

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

If you are using ASP.NET Identity UserManager you can get it like this as well:

var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();

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

If you have changed key for user from Guid to Int for example use this code:

var roles = userManager.GetRoles(User.Identity.GetUserId<int>());
躲猫猫 2024-10-04 00:22:19

我发现这个问题是由于其中提到的异常。我的 Web.Config 没有任何 标记。我意识到,即使我添加了它(如 Infotekka 建议),它最终也会出现数据库异常。按照此处其他答案中的建议后,没有一个完全解决问题。

由于这些Web.Config标签是可以自动生成的,所以通过手动添加来解决感觉不太对劲。如果您遇到类似情况,请撤消对 Web.Config 和 Visual Studio 中所做的所有更改:

  1. Ctrl+Q,输入 nuget 然后单击“管理 NuGet 包”;
  2. Ctrl+E,输入 providers,列表中应显示“Microsoft ASP.NET Universal Providers Core Libraries” strong>”和“Microsoft ASP.NET Universal Providers for LocalDB”(均由 Microsoft 创建);
  3. 单击两者中的“安装”按钮并关闭 NuGet 窗口;
  4. 检查您的 Web.config,现在您应该在 ProfileMembership 中至少有一个 标记SessionState 标签以及新的 RoleManager 标签内,如下所示:

    
        <提供商>
           <添加名称=“DefaultRoleProvider”类型=“System.Web.Providers.DefaultRoleProvider,System.Web.Providers,版本= 2.0.0.0,Culture=neutral,PublicKeyToken=NUMBER”connectionStringName=“DefaultConnection”applicationName=“/”/ >
        
    
    
  5. 添加 enabled="true" 如下所示:

    
    
  6. F6 进行构建,现在应该可以继续进行数据库更新了没有这个例外:

    1. Ctrl+Q,输入 ma​​nager,点击“Package Manager Console”;
    2. 输入update-database -verbose,Seed 方法将运行得很好(如果您没有在其他地方搞乱的话),并在数据库中创建一些表;
    3. Ctrl+W+L 打开服务器资源管理器,您应该能够签入数据连接 >默认连接> 在新创建的表中列出RolesUsersInRoles表!

I found this question due the exception mentioned in it. My Web.Config didn't have any <roleManager> tag. I realized that even if I added it (as Infotekka suggested), it ended up in a Database exception. After following the suggestions in the other answers in here, none fully solved the problem.

Since these Web.Config tags can be automatically generated, it felt wrong to solve it by manually adding them. If you are in a similar case, undo all the changes you made to Web.Config and in 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!
伴随着你 2024-10-04 00:22:19
<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>
<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>
魔法唧唧 2024-10-04 00:22:19

以下是您需要放入 MVC5 及更高版本中的帐户控制器中的代码
获取用户的角色列表:

csharp
公共异步任务RoleAdd(字符串用户ID)
{
返回视图(等待
UserManager.GetRolesAsync(UserID)).OrderBy(s => s).ToList());
}

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

Here is the code that you need to put in your Account Controller in MVC5 and later
to get the list of roles of a user:

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

There is no need to use Roles.GetRolesForUser() and enable the Role Manager Feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文