Active Directory -用户角色
本文关键字:角色 用户 Directory Active | 更新日期: 2023-09-27 18:12:59
我了解如何使用User.Identity
和User.IsInRole
是否有一种方法可以查看用户所处的所有角色?
我们有很多组,有些人在很多组中,但我不想写一个User。IsInRole 20+ times.
在活动目录上下文中,您引用的角色实际上是用户所属的安全(或授权)组。
所以如果你使用。net 3.5或更高版本,你应该检查System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。阅读这里的所有内容:
- 管理。net Framework 3.5中的目录安全主体
- System.DirectoryServices.AccountManagement上的MSDN文档
基本上,您可以定义域上下文并轻松地在AD中查找用户和/或组:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// get the authorization groups - those are the "roles"
var groups = user.GetAuthorizationGroups();
foreach(Principal principal in groups)
{
// do something with the group (or role) in question
}
}
}
新的S.DS.AM使在AD中与用户和组进行交互变得非常容易!