简体   繁体   中英

Get Global Catalog for a given LDAP server

Question first, explanation later: How can I get the GC server for any given LDAP server?

To understand my needs, let me explain:

I had to extend Henning Krause's ExchangeAddressListService (I am not sure whether I should/may c'n'p all of Henning's code into this post?) to get useful debug output:

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;
}

and to allow for selection of a certain domain:

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);
        }
    ...
}

Now I have a problem with the domain because it seems as if for some domains SOMEDOMAIN the Global Catalog cannot be accessed via GC://SOMEDOMAIN . This is my code I use:

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());
}

It produces the error log:

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.

Not all DC are GC. So GC://SOMEDOMAIN may fail if SOMEDOMAIN is not a GC.
In my project, I use the DsGetDcName Win32 function to discover the GC.

Details of DsGetDcName function:

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

See below for how to pinvoke the call:

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

As I know System.DirectoryServices.ActiveDirectory also provide classes to handle GC.
eg Forest.GlobalCatalogs
I already use the DsGetDcName function, so never tried this before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM