【问题标题】:Correct method to search for AD user by email address from .NET通过 .NET 的电子邮件地址搜索 AD 用户的正确方法
【发布时间】:2011-02-01 21:32:02
【问题描述】:

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

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

方法一:使用邮件属性:

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(mail=" + email + ")";
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();

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

我尝试更改电子邮件地址输入的大小写,但仍然没有返回结果。这里有区分大小写的问题吗?如果是,最好的解决方法是什么?

【问题讨论】:

  • 我想我找到了问题所在。 DirectoryEntry.Path 的范围为特定域。我更改了代码以使用全局目录并且搜索正常。我很快就会回来更新。如果有人要添加任何内容,欢迎就邮件与 proxyAddresses 进行回答。

标签: c# .net search active-directory


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果您使用的是 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));
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        相关资源
        最近更新 更多