Validating user credentials against active directory

R

Raghu

I have following code that validates a given user credentails against a
active directory. The login part works but I can not search as it fails to
return
the record. Does any one have any idea what is wrong?

public void Login(string user, string pwd, string domain)
{
string path = "<<my ldap path>>";

DirectoryEntry domainEntry = new DirectoryEntry(path);
domainEntry.Username = domain + @"\" + user;
domainEntry.Password = pwd;

domainEntry.AuthenticationType = AuthenticationTypes.Secure;

string searchFilter = "sAMAccountName=" + user;

DirectorySearcher searcher = new DirectorySearcher(domainEntry,
searchFilter);

searcher.SearchScope = SearchScope.Subtree;

//The following line throws exception if supplied user
credentails are not valid
SearchResult result = searcher.FindOne();
Console.WriteLine("Login succeeded.");

if (result == null)
{
Console.WriteLine("Search for user failed.");
Console.WriteLine();
}
else
{
Console.WriteLine("Search for user succeeded.");
}
}

Thanks.
Raghu/..
 
R

Raghu

Found the answer. Needed to use following filter.

string searchFilter = String.Format("(&(objectClass=user)(sAMAccountName=
{0}))", user);

string searchFilter = String.Format("(&(objectClass=user)(sAMAccountName=
{0}))", user);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top