获取任何用户的所有角色(是的,不是当前登录的) [英] Get all roles of any user (yes, not currently logged one)

查看:151
本文介绍了获取任何用户的所有角色(是的,不是当前登录的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个MVC应用程序,管理员具有管理用户CRUD控制器。现在,需要编辑部分的功能​​进行扩展,它涉及加入多种作用取决于标签。它们依赖于观看用户的角色,而不是对谁正在观看他们的管理员的角色。为实现这一目标,会得到该用户的所有角色作为字符串(或类似)的数组,但实际上,我怎么去获得这些最简单的方法。

In an MVC app, administrator has a CRUD controller for managing users. Now, the functionality of the edit part needs to be extended and it involves adding a number role dependent tabs. They depend on the role of the viewed user, rather than on roles of the administrator who is viewing them. The easiest way for achieving this, would be getting all roles of that user as a array of strings (or similar), but how do I actually go about obtain those.

有没有获得在SimpleMembership单个用户的所有角色的preferred方法(根据他的用户ID )还是我只需要修补存储函数在数据库中,并通过它拉那些?

Is there a preferred method of getting all roles of a single user in SimpleMembership (based on his UserId) or do I just have to patch up a stored function in the database and pull those through it?

编写函数不是什么大不了的事,但这个问题听起来不像是我应该要做出的解决方法。

Writing the function is not a big deal, but this problem doesn't sound like something I should have to make workarounds for.

推荐答案

使用的Roles.GetRolesForUser()方法的 https://msdn.microsoft.com/en-us/library/8h930x07(v = vs.110)的.aspx

Use the Roles.GetRolesForUser() method https://msdn.microsoft.com/en-us/library/8h930x07(v=vs.110).aspx

string[] rolesArray = Roles.GetRolesForUser("username"); 

通过字符串是载于ASPNETDB的用户的用户名。

With the string being the User Name of the user as contained in the aspnetdb.

如果你想使用GUID找到,你可以尝试以下方法:

If you want to find by using a guid, you could try the following:

Guid userId; // ID of user - you can populate this somehow
MembershipUser memUser = Membership.GetUser(userId);
string[] roles = Roles.GetRolesForUser(memUser.UserName);

这篇关于获取任何用户的所有角色(是的,不是当前登录的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆