通过.NET得到Active Directory组用户名 [英] get user names in an Active Directory Group via .net
本文介绍了通过.NET得到Active Directory组用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面code让我的用户组中,但它返回
CN =约翰逊\,汤姆,OU =用户,OU =主,DC =公司,DC = com的
我想只返回姓和名。我怎样才能做到这一点?
的DirectoryEntry OU =新的DirectoryEntry();
DirectorySearcher从SRC =新DirectorySearcher从();
src.Filter =((及(对象类=组)(CN =的gname)));
信息搜索结果RES = src.FindOne();
如果(RES!= NULL)
{
的DirectoryEntry deGroup =新的DirectoryEntry(res.Path);
PropertyCollection pcoll = deGroup.Properties;
的foreach(在deGroup.Properties obj对象[成员])
{
ListBox1.Items.Add(obj.ToString());
}
}
解决方案
使用类System.DirectoryServices.AccountManagement我preFER:
PrincipalContext principalContext =新PrincipalContext(ContextType.Domain);
GroupPrincipal组= GroupPrincipal.FindByIdentity(principalContext的gname);
通过group.Members房产搜索,直到你有一个主要你想要的。然后提取这个名字是这样的:
的foreach(在group.Members主要负责人)
{
字符串名称= principal.Name;
}
The below code gets me the users in the group but it is returned
"CN=johnson\,Tom,OU=Users,OU=Main,DC=company,DC=com"
I want to just return the First and Last name. How can I accomplish this?
DirectoryEntry ou = new DirectoryEntry();
DirectorySearcher src = new DirectorySearcher();
src.Filter = ("(&(objectClass=group)(CN=Gname))");
SearchResult res = src.FindOne();
if (res != null)
{
DirectoryEntry deGroup = new DirectoryEntry(res.Path);
PropertyCollection pcoll = deGroup.Properties;
foreach (object obj in deGroup.Properties["member"])
{
ListBox1.Items.Add(obj.ToString());
}
}
解决方案
I prefer using the classes in System.DirectoryServices.AccountManagement:
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "GName");
Search through the group.Members property until you have a Principal that you want. Then extract the name like this:
foreach (Principal principal in group.Members)
{
string name = principal.Name;
}
这篇关于通过.NET得到Active Directory组用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文