Active Directory:无法使用 DirectorySearcher 联系服务器

Car*_*sen 2 c# ldap active-directory windows-authentication directorysearcher

我不断收到错误无法联系服务器。当我尝试运行我的代码时:

我已经搜索了几个小时,但仍然无法联系服务器。

DirectorySearcher directorySearcher = new DirectorySearcher();
string path = directorySearcher.SearchRoot.Path;
DirectoryEntry directoryEntry = new DirectoryEntry(path);

PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain, "LDAP://domain.dk/DC=domain,DC=dk");
//GroupPrincipal group = GroupPrincipal.FindByIdentity(pricipalContext, "(CN=" + department + ")");
GroupPrincipal group = GroupPrincipal.FindByIdentity(pricipalContext, "(CN=" + department + ")");


if (group != null)
{
    foreach (Principal principal in group.Members)
    {
        UserPrincipal tu = principal as UserPrincipal;

        DirectoryEntry de = tu.GetUnderlyingObject() as DirectoryEntry;
        var store = de.InvokeGet("physicalDeliveryOfficeName").ToString();
        var storeNumber = de.InvokeGet("description").ToString();

        employees.Add(new AdEmployees() { name = principal.Name, phone = tu.VoiceTelephoneNumber, email = tu.EmailAddress, store = store.ToString(), storeNumber = storeNumber.ToString(), link = GenerateLink(principal.Name) });
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我将 AD 所在的域更改为域。

T-H*_*ron 5

这里的关键语句似乎是“我将 AD 所在的域更改为域。”

  1. 确保应用程序服务器指向正确的 DNS 服务器。
  2. 确保客户端指向正确的 DNS 服务器。
  3. 这个连接字符串看起来不对: PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain, "LDAP://domain.dk/DC=domain,DC=dk");

  4. #3 中的连接字符串可能像这样工作得更好:

    PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain, "DOMAIN", "DC=domain,DC=dk");