Active Directory Authencation without using Password.http://forums.asp.net/t/1806248.aspx/1?Active+Directory+Authencation+without+using+Password+Tue, 22 May 2012 13:20:37 -040018062484991904http://forums.asp.net/p/1806248/4991904.aspx/1?Active+Directory+Authencation+without+using+Password+Active Directory Authencation without using Password. <p>Hello friends,</p> <p></p> <p>I am using the following code for Authenticating the user through Active Directory.</p> <p>In it we need the password of the user.</p> <p>Is it possible to retrive the password of the user from AD.</p> <p>If not can this code be modified to use only UserName to authencate the user.</p> <p>Pls find the code below.</p> <p>I need the Solution on urgent basis pls reply with your valuable comments.</p> <p></p> <p>Thanks</p> <p>Harsh Tyagi..</p> <p></p> <p>IsAuthenticated(string domain, string username, string pwd)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //string Username =&nbsp; domain &#43; @&quot;\&quot; &#43;username;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string Username = username;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DirectoryEntry entry = new DirectoryEntry(_path, Username, pwd, AuthenticationTypes.Secure);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Bind to the native AdsObject to force authentication.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object obj = entry.NativeObject;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DirectorySearcher search = new DirectorySearcher(entry);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; search.Filter = &quot;(SAMAccountName= &quot; &#43; username &#43; &quot;)&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; search.PropertiesToLoad.Add(&quot;cn&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SearchResult result = search.FindOne();<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null == result)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Update the new path to the user in the directory.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _path = result.Path;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _filterAttribute = result.Properties[&quot;cn&quot;][0].ToString();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception ex)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new Exception(&quot;Error authenticating user. &quot; &#43; ex.Message);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> </p> 2012-05-22T10:45:03-04:004991966http://forums.asp.net/p/1806248/4991966.aspx/1?Re+Active+Directory+Authencation+without+using+Password+Re: Active Directory Authencation without using Password. <p>You cannot retrive the password of the user from AD</p> <pre class="prettyprint">DirectoryEntry entry = new DirectoryEntry(&quot;LDAP://DomainName&quot;); DirectorySearcher Dsearch = new DirectorySearcher(entry); String Name=&quot;Alex&quot;; dSearch.Filter = &quot;(&amp;(objectClass=user)(l=&quot; &#43; Name &#43; &quot;))&quot;; foreach(SearchResult sResultSet in dSearch.FindAll()) { // Login Name Console.WriteLine(GetProperty(sResultSet,&quot;cn&quot;)); // First Name Console.WriteLine(GetProperty(sResultSet,&quot;givenName&quot;)); // Middle Initials Console.Write(GetProperty(sResultSet,&quot;initials&quot;)); // Last Name Console.Write(GetProperty(sResultSet,&quot;sn&quot;)); } Public static string GetProperty(SearchResult searchResult, string PropertyName) { if(searchResult.Properties.Contains(PropertyName)) { return searchResult.Properties[PropertyName][0].ToString() ; } else { return string.Empty; } }</pre> <p></p> <p>from above code you can verify that 'Alex' exists in AD or not</p> <p><a href="http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory">http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory</a><br> <br> </p> 2012-05-22T11:28:01-04:004992208http://forums.asp.net/p/1806248/4992208.aspx/1?Re+Active+Directory+Authencation+without+using+Password+Re: Active Directory Authencation without using Password. <p>From your code it would appear you have a login form that the user is to enter their username and password into to be authenticated through AD. If you do not need or want the user to provide their password, you can have them auto authenticate through with their credentials that they are logged on their computers. You can use Application_AuthenticateRequest if you are using forms authentication or WindowsAuthentication_Authenticate if you are using windows authentication in the global.asax file.</p> <p>You can setup a service account in AD and use its login info to provide access in your code instead of providing the user's info.&nbsp;Then use the username of the user to filter AD to see if that account exists and if it does return true. You can grab the user's name with either request.servervariables(&quot;LOGON_USER&quot;) or e.Identity.name.</p> 2012-05-22T13:20:37-04:00