2010-03-29 84 views
10

我在通過搜索其電子郵件地址來查找Active Directory中的用戶的代碼時遇到了一些問題。我嘗試過2種方法,但我有時會發現FindOne()方法在某些場合不會返回任何結果。如果我在Outlook中查看GAL中的用戶,我會看到列出的SMTP電子郵件地址。通過.NET的電子郵件地址搜索AD用戶的正確方法

我的最終目標是確認用戶是否存在於AD中。我只有電子郵件地址作爲搜索條件,所以沒有辦法使用名字或姓氏。

方法1:使用郵件屬性:

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

方法2:代理地址屬性:

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

我試圖改變電子郵件地址輸入的情況下,但它仍然沒有返回結果。區分大小寫是否存在問題?如果是這樣,解決它的最好方法是什麼?

+0

我想我已經找到了問題。 DirectoryEntry.Path被限定爲特定的域。我更改了代碼以使用全局編錄並且搜索正在工作。我會盡快回復並更新。如果有人有任何要添加的東西,歡迎回答郵件與proxyAddresses。 – 2010-03-29 03:36:19

回答

0

我從來沒有遇到任何用戶電子郵件地址區分大小寫搜索問題 - 如果您搜索的地址與ADSIEDIT中顯示的完全相同,會發生什麼情況?它是否在正確裝箱時找到地址?

順便說一句,我一直使用「郵件」屬性,因爲它返回用戶的單個默認傳出電子郵件地址,即使有多個地址附加到帳戶。 「proxyAddresses」屬性實際上是一個多值屬性,您只需搜索以「smtp:」開頭的值(屬性中爲小寫)。但是,用戶可能在他們的AD帳戶上有多個SMTP地址(我們這樣做),因此在這兩者之間,「郵件」屬性可能就是您要查找的內容。

13

如果您使用Exchange Server,proxyAddresses是獲取其電子郵件地址的最可靠方法。主smtp地址由所有大寫字母「SMTP:」表示,其他電子郵件地址將以小寫字母「smtp:」作爲前綴。 「郵件」屬性不一定必須是主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("[email protected]"); 

     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運行它們,因此您可以查看結果以及輕鬆查看對象並查看其所有屬性...

相關問題