简体   繁体   English

如何检查用户在asp.net身份中是否担任过多个角色

[英]How to check user is in many roles in asp.net identity

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"))
{

}

And ApplicationUser 和ApplicationUser

public class ApplicationUser : IdentityUser
{        
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

}

So i want to check if the user is in one of the roles. 所以我想检查用户是否处于角色之一。 eg admin, controbutor, superuser, etc. 例如admin,controbutor,superuser等。

I found this example which seems a good one but i have to put the following changes in web.config to make it work. 我发现这个例子似乎不错,但我必须在web.config中进行以下更改以使其起作用。 I didn't need to do that with my code as i have currently. 我不需要像现在这样用我的代码来做。

roleManager enabled="true"

I try to research more on UserManager.IsInRole but I coundn't find many about it. 我尝试在UserManager.IsInRole上进行更多研究,但我找不到很多关于它的信息。 A bit more reading i understand the post i found is about Simple Membership. 多一点阅读,我了解我发现的关于简单会员资格的帖子。

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 . 特别是我指的是这一行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 =) (我仍然想要标题中提出的问题的解决方案,但我也确实需要理解=)

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. 我不知道UserManager返回的User对象具有Roles属性以及该用户具有的角色集合。

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)))
{

}     

More information on Asp.net Identity membership .. this seems like good resource. 有关Asp.net身份成员身份的更多信息,这似乎是不错的资源。

http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples/ http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples/

Well I guess it all depends on what your requirements are ie use simple membership if you can. 好吧,我想这全都取决于您的要求,即如果可以的话,请使用简单的成员资格。 IF you integrate the membership correctly (Forms Authentication) then you should really use User.IsInRole("Role"). 如果您正确集成了成员身份(表单身份验证),则应真正使用User.IsInRole(“ Role”)。

It looks like by what you have posted that you are using your own custom user objects so in this scenario i would use a custom membership provider there are lots of examples of this on the net here the first 1 i found http://www.codeproject.com/Articles/165159/Custom-Membership-Providers 从您发布的内容来看,您似乎正在使用自己的自定义用户对象,因此在这种情况下,我将使用自定义成员资格提供程序,网上有很多此类示例,在这里我找到的第一个1 http:// www。 codeproject.com/Articles/165159/Custom-Membership-Providers

The idea is to just override all the Membership methods and return your own user data from your service layer. 这个想法只是覆盖所有的Membership方法,并从服务层返回您自己的用户数据。

Hope that helps. 希望能有所帮助。 Lee 背风处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM