Why is it impossible to contact an Active Directory server with an Active Directory server?

I am having some problems accessing Active Directory from my WinForm application. I want to create a user and request a user from Active Directory.

Here is a snippet of code to search for a user:

public bool FindUser(string username) { using (PrincipalContext context = new PrincipalContext( ContextType.Domain, this.domainName, this.DomainUserName, this.DomainPassword)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, username); return (user != null) ? true : false; } } 

I cannot create a PrincipalContext object based on the given arguments. I get this exception:

 Exception: The server could not be contacted. 

and internal exception indicates that

 Inner Exception: The LDAP server is unavailable. 

when the domain is working. I can ping and connect to this domain.

+6
c # active-directory
source share
3 answers

You can use the following code:

 objectPath = "LDAP://CN=SC-5515_2,OU=Forus,DC=**MyDomainName**,DC=no"; public static bool Exists(string objectPath) { return DirectoryEntry.Exists(objectPath); } 

This is the code I used for this. It works great when testing if any objects exist in Active Directory.

+1
source share

You can try the following code.

  public bool FindUser2(string userName) { try { DirectoryContext context = new DirectoryContext( DirectoryContextType.Domain, domainName, domainName + @"\" + domainUserName, domainPassword); DirectoryEntry domainEntry = Domain.GetDomain(context).GetDirectoryEntry(); DirectorySearcher searcher = new DirectorySearcher(domainEntry, "(|(objectCategory=user)(cn=" + domainUserName + "))"); SearchResult searchResult = searcher.FindOne(); return searchResult != null; } catch { return false; } } 
+1
source share

You can also use the System.DirectoryServices.Protocols protocol to access other domains. A bit of a steep learning curve, but much faster and more flexible - for example, you can perform asynchronous searches.

0
source share

All Articles