如何在Active Directory中按全名查找电子邮件?

时间:2010-04-30 00:37:07

标签: search active-directory

我想使用Active Directory搜索用户的电子邮件。可用的是用户的全名(例如,电子邮件为“jdoe@example.com”的电子邮件中的“John Doe”)。从我搜索过的内容来看,this接近我要做的事情 - 除了过滤器设置为“SAMAccountName”,这不是我所拥有的。

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

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

1 个答案:

答案 0 :(得分:3)

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

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

此调用通常包含在几个using语句中,如下所示:

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