如何使用其电子邮件地址属性c#获取Active Directory活动用户

时间:2018-03-16 16:39:43

标签: active-directory

如何使用其电子邮件地址获取活动的Active Directory用户并转换我自己的域用户模型中的用户?我尝试了下面的代码,但它不允许我提取电子邮件地址:

using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();        

0 个答案:

没有答案