如何检查用户是在asp.net的身份很多角色 [英] How to check user is in many roles in asp.net identity
问题描述
您好我需要检查,如果用户是在角色之一,我很困惑与不同版本的code的,我发现。让我告诉你我有什么,此刻第一个
Hi I need to check if a user is in one of the roles and i am confused with different versions of code that i found. Let me show you what i have at the moment first
_manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
var currentUser = _manager.FindById(User.Identity.GetUserId());
if (!_manager.IsInRole(currentUser.Id, "admin"))
{
}
和ApplicationUser
And ApplicationUser
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}
所以我要检查,如果用户是在角色之一。例如管理,controbutor,superuser等等。
So i want to check if the user is in one of the roles. e.g. admin, controbutor, superuser, etc.
我发现<一个href=\"http://stackoverflow.com/questions/14477757/how-can-i-check-if-a-user-is-in-any-one-of-a-few-different-roles-with-mvc4-simpl\">this例如这似乎是一个好一个,但我必须把web.config中的以下更改,使其工作。我也没必要做我的code作为我现在有。
I found this example which seems a good one but i have to put the following changes in web.config to make it work. I didn't need to do that with my code as i have currently.
roleManager enabled="true"
我尝试研究更多关于 UserManager.IsInRole
但我coundn't找到许多关于它的。更位阅读我了解我发现文章是关于简单的会员。
I try to research more on UserManager.IsInRole
but I coundn't find many about it. A bit more reading i understand the post i found is about Simple Membership.
由于我使用的身份,我不应该使用code,我发现(指链路和接受的答案),作为将要混合身份和简单的会员资格?特别我指的是这行 Roles.GetRolesForUser
。是否有身份类似的方法?我想我感到困惑的不同成员的框架和不知道什么是正确的用途。我希望有人能解释我明白。
As I am using Identity, i should not use the code that i found (refers to the link and accepted answer) as that will be mixing of Identity and Simple Membership? Specially i refer to this line Roles.GetRolesForUser
. Is there similar method for Identity? I think I am confused about different membership frameworks and don't know what is the proper uses. I hope someone can explain me to understand.
(我还是想对我提出的问题的称号,但我真的需要理解,以及表示溶液)
(I still want the solution for the question i asked in the title but i really need to understand as well =)
推荐答案
好吧,我发现了一个办法做到这一点。我不知道用户对象通过返回的UserManager有角色财产角色的集合,用户拥有。
Well I found a way to do it. I didn't know User object that returns by UserManager has Roles property with the collection of roles that the user has.
using Microsoft.AspNet.Identity;
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
var roles = new List<string> { "admin", "contributor" };
var currentUser = manager.FindById(User.Identity.GetUserId());
if (currentUser.Roles.Any(u => roles.Contains(u.Role.Name)))
{
}
在Asp.net身份会员更多的信息..这似乎是很好的资源。
More information on Asp.net Identity membership .. this seems like good resource.
http://www.nuget.org/packages/Microsoft.AspNet .Identity.Samples /
这篇关于如何检查用户是在asp.net的身份很多角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!