when I enter a valid username and password I always returned back 'Authenticated' as expected but when I enter a wrong un & pw sometimes the method returns 'Authenticated'.
and when the method returnes 'Not Authenticated' (due to the wrong credentials) the reason is: 'Server is not operational' is this method correct? and is there another way to accomplish the same task?
You are just creating an object actually you are not searching the user and after that you are just setting the flag to true. So every time the object is created flag will be True. Os authentication function is going on.
You can try this code
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
Regards
Sunilsingh
Mark this as "Answer" if this post helps you.
Regards,
SunilSingh
Remember to click Mark as Answer on the post that helps to others.
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,
userName, password);
object nativeObject = entry.NativeObject; and there is no error then it always return true. Either user authenticated or not. So change your code and put one if block after executed code above and check there user authenticated or not and set the variable there.
mz1378
Member
398 Points
210 Posts
Authenticating users in Active Directory (C#)
Sep 27, 2010 07:19 AM|LINK
Hi,
I authenticate users using the following code that I earn from this link: http://www.codeproject.com/KB/system/everythingInAD.aspx#34
private bool Authenticate(string userName, string password, string domain) { bool authentic = false; try { DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password); object nativeObject = entry.NativeObject; authentic = true; } catch (DirectoryServicesCOMException) { } return authentic; }when I enter a valid username and password I always returned back 'Authenticated' as expected but when I enter a wrong un & pw sometimes the method returns 'Authenticated'.
and when the method returnes 'Not Authenticated' (due to the wrong credentials) the reason is: 'Server is not operational' is this method correct? and is there another way to accomplish the same task?
sunilsingh
Member
132 Points
64 Posts
Re: Authenticating users in Active Directory (C#)
Sep 27, 2010 08:43 AM|LINK
Hi,
You are just creating an object actually you are not searching the user and after that you are just setting the flag to true. So every time the object is created flag will be True. Os authentication function is going on.
You can try this code
public bool IsAuthenticated(String domain, String username, String pwd) { String domainAndUsername = domain + @"\" + username; DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); try { //Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne(); if(null == result) { return false; } //Update the new path to the user in the directory. _path = result.Path; _filterAttribute = (String)result.Properties["cn"][0]; } catch (Exception ex) { throw new Exception("Error authenticating user. " + ex.Message); } return true; }Regards
Sunilsingh
Mark this as "Answer" if this post helps you.
SunilSingh
Remember to click Mark as Answer on the post that helps to others.
shridhar.wak...
Contributor
2364 Points
464 Posts
Re: Authenticating users in Active Directory (C#)
Sep 27, 2010 08:45 AM|LINK
This method is correct but you can try also using System.DirectoryServices.AccountManagement;
public bool ValidateCredentials(string sUserName, string sPassword)
{
PrincipalContext oPrincipalContext = new PrincipalContext
(ContextType.Domain, domain);
return oPrincipalContext.ValidateCredentials(sUserName, sPassword);
}
{
my experience++;
}
sushilbansal
Member
24 Points
7 Posts
Re: Authenticating users in Active Directory (C#)
Sep 27, 2010 08:46 AM|LINK
After executing of below code
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password); object nativeObject = entry.NativeObject;
and there is no error then it always return true. Either user authenticated or not. So change your code and put one if block after executed code above and check there user authenticated or not and set the variable there.
Regards,
Sushil Bansal
mz1378
Member
398 Points
210 Posts
Re: Authenticating users in Active Directory (C#)
Sep 27, 2010 09:18 AM|LINK
Thank You, I wrote this code and now everything goes good:
private bool AuthenticateAD(string userName, string password, string domain, out string message) { message = ""; DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password); try { object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + userName + ")"; search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne(); if (null == result) { return false; } } catch (Exception ex) { message = ex.Message; return false; //throw new Exception("Error authenticating user. " + ex.Message); } return true; }