如何在Asp.net Identity 3.0和MVC 6中打开角色? [英] How to turn on Roles in Asp.net Identity 3.0 and MVC 6?
问题描述
我不确定我是否在这里错过了什么.默认情况下,即使用户确实具有某些角色,User.IsInRole()
也不起作用.
I am not sure if I am missing something here. By Default User.IsInRole()
does not work even if user DOES HAVE some roles.
我没有自己的角色库实现.我假设默认的应该工作.为了使角色正常工作,我需要在Startup.cs
中做一些特别的事情吗?我正在使用mvc 6 beta 2默认模板.
I don't have my own implementation of role stores. I am assuming that the default ones should work. Is there something special that I need to do in the Startup.cs
to get the roles working? I'm using mvc 6 beta 2 default template.
推荐答案
如果添加这样的角色,则User.IsInRole()
不起作用:
The User.IsInRole()
does not work if I add a role like this:
await UserManager.AddToRoleAsync(user, "Admin");
但是如果我这样做的话,它确实可以工作:
But it does work if I do this:
await UserManager.AddClaimAsync(user, claim: new Claim(ClaimTypes.Role.ToString(), "Admin"));
这篇关于如何在Asp.net Identity 3.0和MVC 6中打开角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!