Reputation: 103
Using Directory Entry in asp.net c#, if I call:
ADUtils newAdClass = new ADUtils("dl-dom", "ad.test", "Password?1");
List<string> domUsers = newAdClass.GetDomainUsers();
----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
public List<string> GetDomainUsers()
{
//returned list
List<string> domainUsers = new List<string>();
//create connection
DirectoryEntry entry = new DirectoryEntry(_lDAPPath, _ldapUser, _ldapPassword);
DirectorySearcher search = new DirectorySearcher(entry);
//search subtree nodes
search.SearchScope = SearchScope.Subtree;
//Active Directory LDAP: All email users (alternate)
search.Filter = "(&(objectClass=user)(objectcategory=person))";
//create results objects from search object
SearchResultCollection results = search.FindAll();
//run through list, for each entry remove 'CN=' and add 'user' to list
for (int i = 0; i < results.Count; i++)
{
DirectoryEntry de = results[i].GetDirectoryEntry();
string user = de.Name.Replace("CN=", "");
domainUsers.Add(user);
}
return domainUsers;
}
This works fine however upon testing if the user enters a domain that does not exist. e.g.
ADUtils newAdClass = new ADUtils("FAKE-dl-dom", "ad.test", "Password?1");
this throws an error inside my code so im attempting to use http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.exists%28v=vs.110%29.aspx
exists
but DirectoryEntry entry returns an object where as I need to test the string, I think the path is wrong...any ideas?
string entry1 = _lDAPPath + "," + _ldapUser + "," + _ldapPassword;
//entry1 returns: LDAP://DC=dl-dom,ad.test,Password?1
if (DirectoryEntry.Exists(entry1))
{
DirectorySearcher search = new DirectorySearcher(entry);
when I use the above code I get the exception
An invalid dn syntax has been specified.
constructor:
public ADUtils(string LDAPDomain, string ADUser, string ADUserPwd)
{
_lDAPPath = "LDAP://DC=" + LDAPDomain;
_ldapUser = ADUser;
_ldapPassword = ADUserPwd;
}
Upvotes: 0
Views: 2628
Reputation: 1749
For example, assume domain is "example.com"
The path to test should be LDAP://example.com
.
If you do not provide the DN, it will automatically connect to the domain root object. So in above example, the object it actually get is LDAP://example.com/DC=example,DC=com
Upvotes: 0
Reputation: 416121
Don't use the Exists()
function to test before accessing. The LDAP directory is volatile, and can change out from under you. This is a race condition.
Instead, use a try/catch block, and handle the exception if it fails:
try
{
//create results objects from search object
SearchResultCollection results = search.FindAll();
//run through list, for each entry remove 'CN=' and add 'user' to list
for (int i = 0; i < results.Count; i++)
{
DirectoryEntry de = results[i].GetDirectoryEntry();
string user = de.Name.Replace("CN=", "");
domainUsers.Add(user);
}
}
catch(Excpetion e)
{
//add code here to process the error
//after debugging, you may even decide to just swallow the exception
// and return an empty collection
}
Upvotes: 0