获取主要组ID
尝试获取用户所属的所有组,包括主要组:
执行以下操作:
DirectoryEntry entry = new DirectoryEntry(LDAP:/domainXYZ, userx, passwordx);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = String.Format("(&(objectClass=user)(userPrincipalName={0}{1}))", userY, LDAP://domainXYZ);
SearchResultCollection resultColln= searcher.FindOne();
string actualGroupName =string.empty;
string grp ="";
foreach (SearchResult singleRes in resultColln)
{
foreach (object value in singleRes.Properties["memberof"])
{
grp = value.ToString();
Console.WriteLine("group:{0} ", grp);
}
}
这将为我提供除主要组之外的所有组。除了其他组之外,是否可以使用 primaryGroupID
来获取主要组名称?
Trying to get all the groups a user belongs to, INCLUDING the primary group:
Doing something like this:
DirectoryEntry entry = new DirectoryEntry(LDAP:/domainXYZ, userx, passwordx);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = String.Format("(&(objectClass=user)(userPrincipalName={0}{1}))", userY, LDAP://domainXYZ);
SearchResultCollection resultColln= searcher.FindOne();
string actualGroupName =string.empty;
string grp ="";
foreach (SearchResult singleRes in resultColln)
{
foreach (object value in singleRes.Properties["memberof"])
{
grp = value.ToString();
Console.WriteLine("group:{0} ", grp);
}
}
This gives me all the groups except the primary group. Is there a way to get the primary group name, using the primaryGroupID
in addition to the other groups?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用以下搜索过滤器运行另一个搜索
primaryGroupToken
是创建组时由 Active Directory 自动生成的计算属性。分配给用户的primaryGroupID
正在存储该值。实际上,如果您想要一种真正简单的方法,我建议UserPrincipal.GetGroups 非常简单。唯一的问题是,您只能在 .NET 3.5 或更高版本中找到它。
GetGroups
仅返回直接包含您的用户的组,包括其主要组。如果您想获取所有嵌套组,可以使用GetAuthorizationGroups
。You should run another search using the following search filter
primaryGroupToken
is a calculated attribute that automatically generated by Active Directory when the group is created. TheprimaryGroupID
assigned to the user is storing this value.Actually, if you want a really easy way, I would suggest UserPrincipal.GetGroups is really easy. The only thing is that you can find it only in .NET 3.5 or later.
GetGroups
returns you only the group that immediately contains your user, including its primary group. If you want to get all the nested groups, you can useGetAuthorizationGroups
.