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

时间:2010-03-29 03:09:12

标签: c# .net 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();

方法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();

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

3 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分:4)

我发现使用SysInternals ADExplorer非常适合测试/调试Active Directory查询。您可以构建查询并针对Active Directory运行它们,您可以查看结果以及轻松查看对象并查看其所有属性......

答案 2 :(得分:0)

我从未遇到过区分大小写搜索用户电子邮件地址的任何问题 - 如果您搜索地址会发生什么,就像在ADSIEDIT中看到的那样?在正确套装时是否找到了地址?

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