More ActiveDirectory --- Binding as a DN?
-
Hey all, I'm working on an Active Directory web application. I've gotten further than my prior post - if you read my prior post, basically, the problem is this - I have to bind to the LDAP directory as a specific DN to authenticate (the username/password route for AD is not good enough since we are dealing with a non-MS ldap server). The process is this: * bind as an anonymous user * search with a filter of your own devising * obtain a single directoryentry object * bind to this directoryentry with the provided password This seemed like an easy thing to do initially - after all, we do have System.DirectoryServices --- the actual process was a little more involved. I've done the first three and got them working now (see below for the code). I'm now at step 4 - how do I bind to the directory using an existing DirectoryEntry and a password? My first thought was:
DirectoryEntry bound = new DirectoryEntry(me.Path, "", password, AuthenticationTypes.ServerBind);
... but that does not seem to work (i.e. it always returns) - how can I tell if the binding was successful? Code to get to this point:// // Create the base path for the authentication. // string path = _host + "/" + _basedn; DirectoryEntry entry = new DirectoryEntry(path); // // Construct the filter necessary to search the // specified LDAP directory structure // Regex r = new Regex(Regex.Escape("$(login)")); string filter = r.Replace(_filter, username); // // Search for our user // DirectorySearcher dsrch = new DirectorySearcher(entry); dsrch.Filter = filter; dsrch.SizeLimit = 2; SearchResultCollection results = dsrch.FindAll(); // // Check to see if we found the user // if (results.Count == 0) return false; if (results.Count > 1) throw new Exception("Invalid user ID in LDAP Directory"); // // Convert the result into a DirectoryEntry // DirectoryEntry me = results[0].GetDirectoryEntry();
-Adrian