Home All Groups Group Topic Archive Search About

Validating user credentials against active directory

Author
17 Apr 2006 6:33 PM
Raghu
I have following code that validates a given user credentails against a
active directory. The login part works but I can not search 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/..

Author
18 Apr 2006 3:41 PM
Raghu
Found the answer. Needed to use following filter.

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

Show quote
"Raghu" <RaghuNoSpam> wrote in message
news:%23W3yk1kYGHA.2376@TK2MSFTNGP03.phx.gbl...
>I have following code that validates a given user credentails against a
>active directory. The login part works but I can not search 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/..
>

AddThis Social Bookmark Button