2015-01-13 58 views
0

問題第一,稍後解釋:如何爲任何給定的LDAP服務器獲取GC服務器?獲取給定LDAP服務器的全局編錄

要了解我的需要,讓我解釋一下:

我不得不延長Henning Krause's ExchangeAddressListService(我不知道我是否應該/可以c'n'p所有的孔翰寧博士的代碼到這個職位?)來獲取有用調試輸出:

private DirectoryEntry GetDirectoryEntry(string path, string protocol) 
{ 
    var ldapPath = string.IsNullOrEmpty(path) ? string.Format("{0}:", protocol) : string.Format("{0}://{1}", protocol, path); 
    dbg.Add("Getting DirectoryEntry for path " + ldapPath); 
    return new DirectoryEntry(ldapPath); 
} 
public ActiveDirectoryConnection(Debug dbg) 
{ 
    this.dbg = dbg; 
} 

,並允許某個域的選擇:

internal AddressList(string path, ActiveDirectoryConnection connection, string domain) 
{ 
    _Path = path; 
    _Connection = connection; 
    _Domain = domain; 
} 

...

private IEnumerable<AddressList> GetAddressLists(string containerName) 
{ 
    string exchangeRootPath; 
    using (var root = _Connection.GetLdapDirectoryEntry(_Domain+"/RootDSE")) 
    ... 
     foreach (SearchResult addressBook in searchResultCollection) 
     { 
      yield return 
       new AddressList((string)addressBook.Properties["distinguishedName"][0], _Connection, _Domain); 
     } 
    ... 
} 

現在我有一個域的問題,因爲它看起來好像有些域SOMEDOMAIN全球目錄不能通過GC://SOMEDOMAIN訪問。這是我的代碼使用方法:

var domain = User.Identity.Name.Split('\\')[0]; // SOMEDOMAIN\SomeUser -> Domain is SOMEDOMAIN 
dbg.Add("User NETBIOS domain is "+domain); 
AddressListService addressListService = new ExchangeAddressListService(connection,domain); 
IEnumerable<AddressList> addressLists = addressListService.GetGlobalAddressLists(); 
AddressList addressList = addressLists.First() 
try { 
    IEnumerable<SearchResult> searchResults = addressList.GetMembers("displayName", "distinguishedname", "mail") 
} catch(Exception e) { 
    dbg.Add("Error in GetMembers: "+e.Message); 
    return new AjaxAnswer(dbg.Flush()); 
} 

它產生錯誤日誌:

User NETBIOS domain is SOMEDOMAIN 
Getting DirectoryEntry for path LDAP://SOMEDOMAIN/RootDSE 
Getting DirectoryEntry for path LDAP://CN=Microsoft Exchange, CN=Services, CN=Configuration,DC=somedomain,DC=net 
Getting DirectoryEntry for path LDAP://CN=All Global Address Lists,CN=Address Lists Container, CN=MYMAIL,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=somedomain,DC=net 
Getting DirectoryEntry for path LDAP://CN=Default Global Address List,CN=All Global Address Lists,CN=Address Lists Container,CN=MYMAIL,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=somedomain,DC=net 
Getting DirectoryEntry for path GC://SOMEDOMAIN 
Error in GetMembers: The server is not operational. 

回答

1

並非所有的DC都是GC。因此如果SOMEDOMAIN不是GC,則GC://SOMEDOMAIN可能會失敗。
在我的項目中,我使用DsGetDcName Win32函數來發現GC。 DsGetDcName函數的

詳情:

http://msdn.microsoft.com/en-us/library/ms675983%28v=vs.85%29.aspx

請參閱以下如何的PInvoke呼叫:

http://www.pinvoke.net/default.aspx/netapi32.dsgetdcname

據我所知System.DirectoryServices.ActiveDirectory還提供類來處理GC。
例如Forest.GlobalCatalogs
我已經使用了DsGetDcName函數,所以從來沒有嘗試過。