views:

418

answers:

4

hi. i am facing some problems in accessing Active Directory from my winform app. what I want is to create a user and query user from Active Directory.

here is code snippet for find 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 am unable to create object of PrincipalContext based on given arguments. i am getting this exception "{"The server could not be contacted."}"

and inner exception states that "{"The LDAP server is unavailable."}"

where as domain is running. i can ping to it and can also connect to this domain.

any suggestion about these exceptions?

A: 

You can use the following code:

public static bool Exists(string objectPath)
{
    bool found = false;
    if (DirectoryEntry.Exists(objectPath))
    {
        found = true;
    }
    return found;
}

objectPath = "LDAP://CN=SC-5515_2,OU=Forus,DC=MyDomainName,DC=no";

This is the code i use for this. Works fine on testing if any object exists in Active directory

EKS
I am getting error in connection with specified Active Directory.
Mohsan
You have to post more information then. The code works, so if its not then your proberbly passing the wrong parameter
EKS
for my local domain it is working fine. but i can not create users in this domain. so that for testing purpose i installed windows 2003 server on virtual machine, configured domain controller. for this virtual machine i am unable to query Active Directory.
Mohsan
Is it necessary to be part of domain to which we want to query from our code? currently my computer is part of "Care" domain where as my virtual machine have "TestDomain"..
Mohsan
Yes, or you have to authenticate against the other domain.
EKS
i am passing username and password to authenticate against other domain
Mohsan
A: 

Mohsan, I know that this response is a little late, but are you still having the same problem? Are you trying to read user information, or write user information to Active Directory?

Sanch01R
-1 This should have been a comment to the question instead of an answer.
Will Marcouiller
A: 

You can try next 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;
        }
    }
lerthe61
A: 

You can also consider using System.DirectoryServices.Protocols for accessing other domains. Bit of a steep learning curve but much faster and more flexible - e.g. you can do proper asynchronous searches.

serialhobbyist