如何从 Active Directory 获取用户的电子邮件地址?

How to get a user#39;s e-mail address from Active Directory?(如何从 Active Directory 获取用户的电子邮件地址?)
本文介绍了如何从 Active Directory 获取用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图在 AD 中获取用户的电子邮件地址,但没有成功.

I am trying to get a user's email address in AD without success.

String account = userAccount.Replace(@"Domain", "");
DirectoryEntry entry = new DirectoryEntry();

try {
    DirectorySearcher search = new DirectorySearcher(entry);

    search.PropertiesToLoad.Add("mail");  // e-mail addressead

    SearchResult result = search.FindOne();
    if (result != null) {
        return result.Properties["mail"][0].ToString();
    } else {
        return "Unknown User";
    }
} catch (Exception ex) {
    return ex.Message;
}

有人能看到问题或指出正确的方向吗?

Can anyone see the issue or point in the right direction?

推荐答案

免责声明:此代码不搜索 单个完全匹配,所以对于 domainj_doe 它可能会返回domainj_doe_from_external_department 的电子邮件地址(如果也存在类似名称的帐户).如果此类行为不受欢迎,则使用 samAccountName 过滤器而不是 anr 下面使用或过滤结果 另外.

Disclaimer: This code doesn't search for a single exact match, so for domainj_doe it may return domainj_doe_from_external_department's email address if such similarly named account also exists. If such behaviour is undesirable, then either use a samAccountName filter intead of an anr one used below or filter the results additionally.

我已成功使用此代码(其中帐户"是不带域的用户登录名(域帐户):

I have used this code successfully (where "account" is the user logon name without the domain (domainaccount):

// get a DirectorySearcher object
DirectorySearcher search = new DirectorySearcher(entry);

// specify the search filter
search.Filter = "(&(objectClass=user)(anr=" + account + "))";

// specify which property values to return in the search
search.PropertiesToLoad.Add("givenName");   // first name
search.PropertiesToLoad.Add("sn");          // last name
search.PropertiesToLoad.Add("mail");        // smtp mail address

// perform the search
SearchResult result = search.FindOne();

这篇关于如何从 Active Directory 获取用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to determine the type (AD User vs. AD Group) of an account?(如何确定帐户的类型(AD 用户与 AD 组)?)
How to resolve quot;The server does not support the control. The control is critical.quot; Active Directory error(如何解决“服务器不支持控件.控制至关重要.活动目录错误)
How to authenticate users with a customer#39;s (remote) active directory server(如何使用客户的(远程)活动目录服务器对用户进行身份验证)
How to know if my DirectoryEntry is really connected to my LDAP directory?(如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?)
Add member to AD group from a trusted domain(将成员从受信任的域添加到 AD 组)
How to retrieve Users in a Group, including primary group users(如何检索组中的用户,包括主要组用户)