2010-04-30 29 views
0

我想通过使用Active Directory搜索用户的电子邮件。可用的是用户的全名(例如,“John Doe”用于具有电子邮件“[email protected]”的电子邮件)。从我搜索的内容来看,this接近我所要做的 - 除了过滤器设置为“SAMAccountName”,这不是我所拥有的。如何在Active Directory中按全名查找电子邮件?

除非我误解,我只需要选择正确的属性并以相同的方式折腾全名。不幸的是,我不知道这个属性是什么,显然没有人需要以这种方式搜索信息,这是一个非常大的列表(msdn * microsoft * com/en-us/library/ms675090(v = VS.85)* aspx,stackoverflow没有让我链接2个超链接,因为我没有10个代表)的属性。

有谁知道如何使用用户的全名通过Active Directory查找获取用户的电子邮件地址?

回答

3

使用3.5 System.DirectoryServices.AccountManagement.dll,您可以使用UserPrincipal FindByIdentity方法:

UserPrincipal.FindByIdentity(context, IdentityType.Name, "Lastname, Firstname"); 

此调用通常裹一对夫妇使用的语句像下面的内部:

using (var ctx = new PrincipalContext(ContextType.Domain)) 
{  
    using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.Name, user)) 
    { 
     return userPrincipal == null ? "" : userPrincipal.EmailAddress; 
    } 
} 
相关问题