通过.NET得到Active Directory组用户名用户名、NET、Active、Directory

由网友(向前走别回头)分享简介:下面code让我的用户组中,但它返回CN =约翰逊\,汤姆,OU =用户,OU =主,DC =公司,DC = com的 我想只返回姓和名。我怎样才能做到这一点?的DirectoryEntry OU =新的DirectoryEntry();DirectorySearcher从SRC =新DirectorySearch...

下面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;
}
 
active directory怎么创建用户

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;
}

阅读全文

相关推荐

最新文章