c# - 使用 C# 中的电子邮件地址在 AD 中搜索分发列表
问题描述
我正在尝试验证活动目录中的电子邮件地址。我偶然发现了一个使用 System.DirectoryServices 的解决方案。现在我可以验证普通用户,但此代码不适用于通讯组。
DirectorySearcher search = new DirectorySearcher(filter: "(mail="+username+"*)");
SearchResultCollection results = search.FindAll();
我在这里做错了什么?我要验证的通讯组的名称类似于“我的组”,别名“我的组”。如果我做类似的事情,我可以找到该组
DirectorySearcher search = new DirectorySearcher(filter: "cn=My Group");
问题是,即使我这样做,我也无法在其属性中找到它的电子邮件。更大的问题是,在我的代码中,我没有群组的 cn,只有用于搜索的电子邮件别名。
有任何想法吗?提前致谢!
解决方案
在 Active Directory 中,“mail”是一个包含电子邮件地址的单值属性。有一个多值属性 proxyAddresses,它包含所有条目的电子邮件地址,而 Exchange 实际使用的正是这个值。虽然大多数用户在邮件和代理地址中都有一个电子邮件地址,但情况并非总是如此。根据邮件验证电子邮件地址可能会产生错误的失败。例如,在我工作的地方,有人更改了他们的名字,他们在 proxyAddresses 中保留了他们的旧电子邮件地址 90 天,以允许他们将他们的新地址传达给联系人。它仍然是个人的有效地址,但仅查看邮件就会说该地址无效。
proxyAddresses 中的值以传输方式为前缀(通常为“smtp”,因为邮件流的优势是基于 SMTP 的......尽管可能使用其他一些传输方式——搜索已知记录并返回 proxyAddresses 以查看正在使用的内容在您的目录中)。
假设 SMTP 是传输,通过其 proxyAddresses 值之一查找帐户的过滤器是:
(&(proxyAddresses=smtp:me@example.com))
这是我使用的一个快速示例控制台应用程序,它成功地在我的域中找到了安全组和通讯组。
static void Main(string[] args)
{
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://ADServer.example.com","lisa@example.com","!P@ssw0rdG03sH3r3!", AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(directoryEntry)
{
PageSize = int.MaxValue,
// Filter = "(&(mail=LJRTestDistroGroup@example.com))"
Filter = "(&(proxyAddresses=smtp:LJRTestSecurityGroup@example.com))"
};
searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("proxyAddresses");
searcher.PropertiesToLoad.Add("mail");
SearchResultCollection result = searcher.FindAll();
List<string> names = new List<string>();
foreach (SearchResult r in result)
{
Console.WriteLine(r.Properties["displayname"][0].ToString());
Console.WriteLine(r.Properties["mail"][0].ToString());
Console.WriteLine(r.Properties["proxyAddresses"][0].ToString());
}
}
推荐阅读
- java - Jboss EAP 7 部署失败 - "{\"WFLYCTL0080: 失败的服务\" => {\"jboss.deployment.unit.\\\"OptionsManagementREST.war\\\"
- javascript - Webpack Encore 未在基本模板中加载 JavaScript
- html - 试图找出带有背景图像和部分块的布局
- node.js - 如何使用node.js将对API调用的JSON响应的值存储在与(未知)对象数量一样多的变量中
- ruby - Ruby 多个代码块如何协同工作/链接时如何工作?
- c++ - 从 ROOT 更改目录 (C++)
- javascript - 如何通过网站在 IOS 设备上录制音频
- pyspark - 如何使用 Pyspark/SQL/DataFrames SPARK RDD 更新/删除 DB2 源表数据?
- react-native - 尝试从相机 (expo) 拍照时出现 takePicture 失败错误
- testing - 与事件溯源系统的集成测试