c# adsi errors

R

Rubble

Hello, Im trying to see if a user exists in active directory. The
code I have has been posted elsewhere and apparantly works for
others....but it will not work for me. I get the error: "The
specified domain either does not exist or could not be contacted."
Can anyone please help me? Im completely lost here.

Here is the code (roughly the same code works when I create it in
vbs):

string LDAPPath = "LDAP://DC=mydomain,DC=mydc,DC=mydc2,DC=mydc3,DC=com";

DirectoryEntry de = new DirectoryEntry();

de.Path = LDAPPath.ToString();

DirectorySearcher searcher = new DirectorySearcher();

searcher.SearchRoot = de;

searcher.Filter = "(name=clfield)";

searcher.PageSize = 10;

searcher.SearchScope = SearchScope.Subtree;

searcher.PropertiesToLoad.Add("name");

searcher.PropertiesToLoad.Add("userAccountControl");

SearchResultCollection results = searcher.FindAll();
string szTest = "";

foreach(SearchResult result in results)

{

szTest = result.Properties["name"][0].ToString();

}


Thanks - B. Rubble
 
M

Marc Scheuner [MVP ADSI]

Hello, Im trying to see if a user exists in active directory. The
code I have has been posted elsewhere and apparantly works for
others....but it will not work for me. I get the error: "The
specified domain either does not exist or could not be contacted."

Does your domain REALLY consist of all of these FIVE elements??
string LDAPPath = "LDAP://DC=mydomain,DC=mydc,DC=mydc2,DC=mydc3,DC=com";

Try downloading my ADSI Browser from http://www.lupus.ch/adsi (bottom
of the page) - it shows you your AD hierarchy - go to your domain -
what does the AD path for that domain look like?? Usually, there's at
most 3 dc= elements - five is very unusual.

Also, I'd probably rather just try and bind to the user in question,
rather than doing a directory search....
string LDAPPath = "LDAP://DC=mydomain,DC=mydc,DC=mydc2,DC=mydc3,DC=com";
DirectoryEntry de = new DirectoryEntry();
de.Path = LDAPPath.ToString();

????? LDAPPath already *IS* a string - why do a .ToString() on a
string??

I'd prefer:

DirectoryEntry de = new DirectoryEntry(LDAPPath); - that's it

Or better yet - if you know your user is called "John Doe", and lives
in the "Users" container of your domain, just bind to:

DirectoryEntry deUserInQuestion = new DirectoryEntry("LDAP://cn=John
Doe,cn=Users,dc=fabrikam,dc=com");

if(deUserInQuestion != null)
// user exists
else
// user doesn't exist

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
M

Marc Scheuner [MVP ADSI]

Hello, Im trying to see if a user exists in active directory.

Or better yet - use the static method "Exists" to test for a user's
existence, given its LDAP path (from MSDN):

-------------------------------------------------------
You can use the Exists method if you would like to verify an entry is
in the directory. This method is provided in the DirectoryEntry class.
The following code example shows how to use Exists.

if (DirectoryEntry.Exists("LDAP://CN=John
Doe,CN=Users,DC=fabrikam,DC=com"))
Console.WriteLine("object exists");
else
Console.WriteLine("object does not exist");

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
R

Rubble

Thanks! These replies are excellent info.

I have a follow up though if someone would happen to have the
expertise....

I implemented the suggestions above...noticed I got the same error, so
I converted the project to a console app and it worked great. I then
realized that I cant access AD stuff through my asp.net code.

The old way -- I would compile the code into a dll and put it in COM+
and run it under an account that had rights to browse AD.

How do I do this with .net and C#???

Thanks again for the great suggestions!
B. Rubble.
 
W

Willy Denoyette [MVP]

See inline ****

Willy.

Rubble said:
Thanks! These replies are excellent info.

I have a follow up though if someone would happen to have the
expertise....

I implemented the suggestions above...noticed I got the same error, so
I converted the project to a console app and it worked great. I then
realized that I cant access AD stuff through my asp.net code.

**** Specify valid credentials in the Username and Password properties of your DirectoryEntry object de.
de.Username = "domain\\DomainAccountWithADAccessPrivileges"
de.Password = "hisPassword"

The old way -- I would compile the code into a dll and put it in COM+
and run it under an account that had rights to browse AD.

How do I do this with .net and C#???
**** Use the ".NET Framework Services Installation Utility" regsvcs.exe to register your correctly implemented .NET assembly with
the COM+ catalog.
 
R

Rubble

This looks like the way to go:
**** Use the ".NET Framework Services Installation Utility" regsvcs.exe to register your correctly implemented .NET assembly with
the COM+ catalog.

Because I cant pass a password over the net.


Thanks! I appreciate the advice!

B. Rubble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top