简体   繁体   中英

Active Directory: The server could not be contacted using DirectorySearcher

I keep getting the error The server could not be contacted. When i'm trying to run my code:

I've searched for a few hours, and i'm still unable contact the server.

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

Note: I changed my domain where the AD is located to domain.

The key statement here seems to be "I changed my domain where the AD is located to domain."

  1. Ensure the application server is pointed to the correct DNS server.
  2. Ensure the client is pointed to the correct DNS server.
  3. This connection string looks wrong: PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain, "LDAP://domain.dk/DC=domain,DC=dk");

  4. Connection string in #3 might work better like this:

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

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