如何获得 ADGroup 类型的安全性并分发这两种广告组类型。如何让 AdGroup 类型既安全又分发?看不到任何属性来区分组类型?
GetGroups - 返回组对象的集合,这些对象指定当前主体所属的组。此重载方法仅返回主体直接所属的组;不执行递归搜索。
GetAuthorizationGroups - 返回包含此用户所属的所有授权组的主体对象的集合。此函数仅返回属于安全组的组;不返回通讯组。
public async Task<List<AdGroup>> GetGroups(string userName)
{
List<AdGroup> result = new List<AdGroup>();
// establish domain context
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);
// if found - grab its groups
if (user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
// PrincipalSearchResult<Principal> groups = user.GetGroups();
var i= 1;
// iterate over all groups
foreach (Principal p in groups)
{
// make sure to add only group principals
if (p is GroupPrincipal)
{
result.Add(new AdGroup
{
Id = i,
Name = ((GroupPrincipal)p).Name,
Type= ((GroupPrincipal)p).IsSecurityGroup.Value) ;
});
i++;
}
}
}
return result;
}