C# - 查找 Active Directory 用户的所有电子邮件地址

C# - Find all email addresses for an Active Directory user(C# - 查找 Active Directory 用户的所有电子邮件地址)
本文介绍了C# - 查找 Active Directory 用户的所有电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试获取与给定 AD 用户关联的所有电子邮件地址.

I'm trying to get all the email addresses associated to a given AD user.

对于用户,我拥有域和登录名(例如 DOMAINUserName),并且我的 AD 将电子邮件地址存储在:

For the user I have the domain and the login name (ex. DOMAINUserName) and I the AD is storing the email addresses in:

  1. 邮件属性.
  2. proxyAddresses 属性中.

到目前为止,我不知道使用什么 C# API 来连接到 AD,以及如何由用户正确过滤以获取所有电子邮件地址.我使用的是 .NET 3.5.

So far, I don't know what C# API to use to connect to the AD, and how to properly filter by the user to fetch all the email addresses. I'm using .NET 3.5.

谢谢.

推荐答案

这是使用 System.DirectoryServices 命名空间.

Here's a possible solution using various classes in the System.DirectoryServices namespace.

string username = "username";
string domain = "domain";

List<string> emailAddresses = new List<string>();

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username);

// Add the "mail" entry
emailAddresses.Add(user.EmailAddress);

// Add the "proxyaddresses" entries.
PropertyCollection properties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties;
foreach (object property in properties["proxyaddresses"])
{
   emailAddresses.Add(property.ToString());
}

这篇关于C# - 查找 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(如何检索组中的用户,包括主要组用户)