How to connect and authenticate users with LDAPhttp://forums.asp.net/t/1799404.aspx/1?How+to+connect+and+authenticate+users+with+LDAPFri, 04 May 2012 19:57:25 -040017994044962521http://forums.asp.net/p/1799404/4962521.aspx/1?How+to+connect+and+authenticate+users+with+LDAPHow to connect and authenticate users with LDAP <p>Hi,</p> <p>I am able to connect to the ldap and get the user info even without a password by just putting this in my browser:</p> <p></p> <p>ldap://directory.abc.edu/uid=myUserID,ou=People,o=abc.edu</p> <p>I have used &quot;abc&quot; &quot;myUserID&quot; as just place holder for the actual words as I can't show them here.</p> <p>I have a website that needs to authenticate users from this ldap and don't let them login otherwise. I have written the follwoing code just to test and when I run it I get a dn syntax error exception.</p> <p></p> <p>Can anyone please help me with this and tell me how should I set up the path and the parameters so that I can get a reply from the ldap server:</p> <p></p> <p>this is the code I wrote:</p> <p>using (DirectoryEntry root = new DirectoryEntry())<br> {<br> root.Path = &quot;LDAP://directory.abc.edu&quot;;<br> root.Username = &quot;myDomain\myUserID&quot;; (I even tried it without the domain as I don't use it in browser path but get the response!)<br> root.Password = &quot;myPassword&quot;;</p> <p>using (DirectorySearcher searcher = new DirectorySearcher())<br> {<br> searcher.SearchRoot = root;</p> <p>SearchResultCollection results = searcher.FindAll(); &nbsp;-----&gt; When I run it, on this line I get an exception (see below for exception)<br> StringBuilder summary = new StringBuilder();<br> foreach (SearchResult result in results)<br> {<br> foreach (string propName in result.Properties.PropertyNames)<br> {<br> foreach (string s in result.Properties[propName])<br> {<br> summary.Append(&quot; &quot; &#43; propName &#43; &quot;: &quot; &#43; s &#43; &quot;\r\n&quot;);<br> }<br> }<br> summary.Append(&quot;\r\n&quot;);<br> }<br> Console.WriteLine(summary);<br> }<br> }</p> <p>&nbsp;</p> <p>the exception I get is:</p> <p>System.DirectoryServices.DirectoryServicesCOMException was unhandled</p> <p>&nbsp; Message=An invalid dn syntax has been specified.</p> <p>&nbsp;</p> <p>&nbsp; Source=System.DirectoryServices</p> <p>&nbsp; ErrorCode=-2147016654</p> <p>&nbsp; ExtendedError=13</p> <p>&nbsp; ExtendedErrorMessage=Invalid DN</p> <p>&nbsp; StackTrace:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.DirectoryServices.DirectoryEntry.Bind()</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.DirectoryServices.DirectoryEntry.get_AdsObject()</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.DirectoryServices.DirectorySearcher.FindAll()</p> <p></p> 2012-05-02T19:30:44-04:004965447http://forums.asp.net/p/1799404/4965447.aspx/1?Re+How+to+connect+and+authenticate+users+with+LDAPRe: How to connect and authenticate users with LDAP <p><span>Once you have found the user's DN you should then add those credentials to the first context's environment and then try a reconnect(). That does the LDAP bind operation.</span></p> <p><span><br> </span></p> <p><span>Thank you</span></p> 2012-05-04T10:49:39-04:004965708http://forums.asp.net/p/1799404/4965708.aspx/1?Re+How+to+connect+and+authenticate+users+with+LDAPRe: How to connect and authenticate users with LDAP <p>Hi,</p> <p>Thanks for your reply but it's very vague for me! What is the DN? Did I say that I have it? I can't even connect to it programmatically.</p> <p>Please be more specific. I included my code and the URL I use in the browser for that matter.</p> <p>Please kindly write some code for me. Thanks,</p> 2012-05-04T13:11:59-04:004966261http://forums.asp.net/p/1799404/4966261.aspx/1?Re+How+to+connect+and+authenticate+users+with+LDAPRe: How to connect and authenticate users with LDAP <p>This is a good link for those working with C and ldap, <a href="http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C"> http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C</a>. That exception error indicates the path may be incorrect.</p> <p>When searching ldap for accounts you do not want to have the domain with the user name. So user UserID no domain\UserID</p> 2012-05-04T19:57:25-04:00