C# - Find all email addresses for an Active Directory user(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:
- 邮件属性.
- 在
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 用户的所有电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# - 查找 Active Directory 用户的所有电子邮件地址
- 在 C# 中异步处理项目队列 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
