通过.NET的电子邮件地址搜索AD用户的正确方法

Bri*_*tle 12 .net c# search active-directory

我遇到了一些代码问题,这些代码旨在通过搜索他们的电子邮件地址来查找Active Directory中的用户.我尝试了2种方法,但有时我发现FindOne()方法在某些情况下不会返回任何结果.如果我在Outlook中的GAL中查找用户,我会看到列出的SMTP电子邮件地址.

我的最终目标是确认用户是否存在于AD中.我只有电子邮件地址作为搜索条件,因此无法使用名字或姓氏.

方法1:使用邮件属性:

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(mail=" + email + ")";
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
Run Code Online (Sandbox Code Playgroud)

方法2:proxyAddresses属性:

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(proxyAddresses=SMTP:" + email + ")"; // I've also tried with =smtp:
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
Run Code Online (Sandbox Code Playgroud)

我已经尝试更改电子邮件地址输入的大小写,但它仍然没有返回结果.这里是否存在区分大小写的问题?如果是这样,解决它的最佳方法是什么?

Fel*_*lan 13

如果您使用的是Exchange Server,则proxyAddresses是获取其电子邮件地址的最可靠方法.主smtp地址由所有大写字母"SMTP:"表示,其他电子邮件地址将以小写"smtp:"作为前缀.属性"mail"不一定必须是主SMTP地址,尽管通常是.

以下是我使用的一些代码的变体:

    public static SearchResult FindAccountByEmail(string email)
    {
        string filter = string.Format("(proxyaddresses=SMTP:{0})", email);

        using (DirectoryEntry gc = new DirectoryEntry("GC:"))
        {
            foreach (DirectoryEntry z in gc.Children)
            {
                using (DirectoryEntry root = z)
                {
                    using (DirectorySearcher searcher = new DirectorySearcher(root, filter, new string[] { "proxyAddresses", "objectGuid", "displayName", "distinguishedName" }))
                    {
                        searcher.ReferralChasing = ReferralChasingOption.All;
                        SearchResult result = searcher.FindOne();

                        return result;
                    }
                }
                break;
            }
        }

        return null;
    }

    static void Main(string[] args)
    {
        SearchResult result = FindAccountByEmail("someone@somewhere.com");

        string distinguishedName = result.Properties["distinguishedName"][0] as string;
        string name = result.Properties["displayName"] != null
                        ? result.Properties["displayName"][0] as string
                        : string.Empty;
        Guid adGuid = new Guid((byte[]) (result.Properties["objectGUID"][0]));

        string emailAddress;
        var emailAddresses = (from string z in result.Properties["proxyAddresses"]
                              where z.StartsWith("SMTP")
                              select z);
        emailAddress = emailAddresses.Count() > 0 ? emailAddresses.First().Remove(0, 5) : string.Empty;


        Console.WriteLine(string.Format("{1}{0}\t{2}{0}\t{3}{0}\t{4}",
                      Environment.NewLine,
                      name,
                      distinguishedName,
                      adGuid,
                      emailAddress));
    }
Run Code Online (Sandbox Code Playgroud)


Sql*_*yan 0

我在搜索用户的电子邮件地址时从未遇到过区分大小写的问题 - 如果您搜索的地址与 ADSIEDIT 中显示的完全一样,会发生什么情况?当大小写正确时它能找到地址吗?

顺便说一句,我一直使用“邮件”属性,因为它返回用户的单个默认传出电子邮件地址,即使该帐户附加了多个地址。“proxyAddresses”属性实际上是一个多值属性,您只需搜索以“smtp:”开头的值(在属性中为小写)。但是,用户的 AD 帐户上可能有多个 SMTP 地址(我们这样做),因此在这两者之间,“邮件”属性可能就是您要查找的内容。