角色管理器功能尚未启用
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过读取布尔属性来执行此操作:
这是从
web.config
roleManager 元素的enabled
属性直接读取>:更新:
有关详细信息,请查看此 MSDN 示例:https: //msdn.microsoft.com/en-us/library/aa354509(v=vs.110).aspx
You can do this by reading from the boolean property at:
This is a direct read from the
enabled
attribute of theroleManager
element in theweb.config
:Update:
For more information, check out this MSDN sample: https://msdn.microsoft.com/en-us/library/aa354509(v=vs.110).aspx
如果您来到这里是因为您正在使用新的
ASP.NET
Identity
UserManager
,那么您实际上正在寻找的是RoleManager
:roleManager
将允许您查看角色是否存在、创建等,并且它是为UserManager
创建的If you got here because you're using the new
ASP.NET
Identity
UserManager
, what you're actually looking for is theRoleManager
:roleManager
will give you access to see if the role exists, create, etc, plus it is created for theUserManager
我通过 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.
如果您使用的是 ASP.NET Identity UserManager,您也可以这样获取:
如果您已将用户的密钥从 Guid 更改为 Int,例如使用以下代码:
If you are using
ASP.NET Identity UserManager
you can get it like this as well:If you have changed key for user from Guid to Int for example use this code:
我发现这个问题是由于其中提到的异常。我的 Web.Config 没有任何
标记。我意识到,即使我添加了它(如 Infotekka 建议),它最终也会出现数据库异常。按照此处其他答案中的建议后,没有一个完全解决问题。由于这些Web.Config标签是可以自动生成的,所以通过手动添加来解决感觉不太对劲。如果您遇到类似情况,请撤消对 Web.Config 和 Visual Studio 中所做的所有更改:
检查您的 Web.config,现在您应该在 Profile、Membership、 中至少有一个
标记SessionState 标签以及新的 RoleManager 标签内,如下所示:添加
enabled="true"
如下所示:按 F6 进行构建,现在应该可以继续进行数据库更新了没有这个例外:
update-database -verbose
,Seed 方法将运行得很好(如果您没有在其他地方搞乱的话),并在数据库中创建一些表;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:
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:Add
enabled="true"
like so:Press F6 to Build and now it should be OK to proceed to a database update without having that exception:
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;以下是您需要放入 MVC5 及更高版本中的帐户控制器中的代码
获取用户的角色列表:
csharpRoleAdd(字符串用户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.