获取给定LDAP服务器的全局编录

时间:2015-01-13 15:26:30

标签: c# active-directory

首先提出问题,稍后解释:如何为任何给定的LDAP服务器获取GC服务器?

要了解我的需要,请解释一下:

我不得不延长Henning Krause's ExchangeAddressListService(我不确定我是否应该/可能将所有Henning的代码都写入这篇文章?)以获得有用的调试输出:

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 个答案:

答案 0 :(得分:1)

并非所有DC都是GC。如果GC://SOMEDOMAIN不是GC,则SOMEDOMAIN可能会失败 在我的项目中,我使用DsGetDcName Win32函数来发现GC。

DsGetDcName函数的详细信息:

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

请参阅以下有关如何拨打电话的信息:

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

据我所知System.DirectoryServices.ActiveDirectory也提供了处理GC的类 例如Forest.GlobalCatalogs
我已经使用了DsGetDcName函数,所以从来没有尝试过这个。