有没有一种方法可以获取Windows身份验证用户所在的角色列表,而无需通过WindowsPrincipal.IsInRole
方法进行明确检查?如何检索用户所属的所有角色(组)?
回答
WindowsPrincipal.IsInRole
只是检查用户是否是具有该名称的组的成员;一个Windows Group 是一个角色。您可以从WindowsIdentity.Groups
属性中获取用户所属的组的列表。
你可以从WindowsIdentity
您WindowsPrincipal
:
WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity;
,或者你可以从一个工厂方法得到它的WindowsIdentity:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsIdenity.Groups
是IdentityReference
集合这只是给你的SID的组。如果您需要的组名,您将需要翻译IdentityReference
为NTAccount
,并获得价值:
var groupNames = from id in identity.Groups
select id.Translate(typeof(NTAccount)).Value;
编辑:乔希打我吧! :)
试试这个
using System;
using System.Security.Principal;
namespace ConsoleApplication5
{
internal class Program
{
private static void Main(string[] args)
{
var identity = WindowsIdentity.GetCurrent();
foreach (var groupId in identity.Groups)
{
var group = groupId.Translate(typeof (NTAccount));
Console.WriteLine(group);
}
}
}
}
如果没有连接到域服务器,的翻译功能可能会抛出异常“//The trust relationship between this workstation and the primary domain failed.
” 但是对于大多数群体,这将是好了,personnally我使用:
foreach(var s in WindowsIdentity.GetCurrent().Groups) {
try {
IdentityReference grp = s.Translate(typeof (NTAccount));
groups.Add(grp.Value);
}
catch(Exception) { }
}
这就是答案。 – 2014-12-24 18:20:01
在一个ASP.NET MVC的网站,你可以做这样的:
添加到您的Web.config:
<system.web>
...
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
...
</system.web>
然后你可以使用Roles.GetRolesForUser()
让所有的Windows组用户是的成员。确保你是using System.Web.Security
。
- 1. 如何获取角色中的所有用户,包括角色中的角色?
- 2. 如何使用Auth模块检索Kohana 3中的所有用户和所有角色?
- 3. 如何获取本地用户所属的所有本地组
- 4. CakePHP用户角色检索
- 5. 如何使用linq to sql检查组的所有用户都具有用户角色?
- 6. Microsoft Azure AD oauth标识用户所属的用户角色和组(orgunit)
- 7. 如何获取特定角色的所有用户?
- 8. 如何删除Kohana中的所有用户角色3
- 9. 如何获得来自特定角色的所有用户
- 10. 如何列出页面上特定角色的所有用户?
- 11. 如何将所有用户添加到Parse.com中的角色?
- 12. 检索所有用户asyncronously .NET的MVC
- 13. Django检索用户的所有评论
- 14. 如何获取systemuser的所有角色?
- 15. 无法检索具有所有用户的组
- 16. Firebase Android - 检索有关组中所有用户的信息
- 17. 如何检索使用Python的给定用户的所有Tweets和属性?
- 18. 使用角色掩码来查找所有具有特定角色的用户
- 19. 从Auth0检索所有用户
- 20. Spring Security - 检索用户的角色
- 21. 如何使用NDepend检索.NET方法的所有属性?
- 22. android - 如何检索用户的google +中的所有帖子
- 23. 如何检索我的用户所拥有的权限?
- 24. 如何检索我的域中的所有用户
- 25. 使用rolify查找具有特定角色的所有用户
- 26. C#LDAP查询检索组织单位中的所有用户
- 27. 有没有办法用twig检索当前页面所需的角色?
- 28. Mysql - 如何检索没有特定属性的所有项目?
- 29. 如何检索客户端使用jqgrid的所有数据
- 30. Active Directory - 如何检索用户的所有架构条目
我用`var identity = User.Identity作为WindowsIdentity;` – Jaider 2014-01-20 15:40:48