Click here to Skip to main content
15,846,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the below method to fetch user detail from active directory in different domain. when i am using the application in one of our domain which encounters errors, I am attaching the error message and code below

error =>"Logon failure: unknown user name or bad password."

string strName;
strName = Request.ServerVariables["AUTH_USER"];
string[] UserDetail = strName.Split('\\');
string Str = "LDAP://" + domainname;
DirectoryEntry entry = new DirectoryEntry(Str);
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(sAMAccountName=" + username + ")";
SearchResult sresult = Dsearch.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
lblfname.Text = dsresult.Properties["givenName"][0].ToString();
lbllname.Text = dsresult.Properties["sn"][0].ToString();

Any suggestion/help on this would be really appreciated
Updated 15-Jun-12 0:15am

Are you under the domain that you are trying to access? If not, that can be the reason to start with. Make sure that the website runs under the same domain account. Impersonation might help.

Also, see if the AD Username is properly specified: (DOMAIN\username)

In general, you get this error message when a logon attempt failed if the given password was bad, if the user account does not exist in that domain or if the password is expired, or if the password must be changed.

Refer this blog post for full details: MSDN Blog: Logon failure- unknown user name or bad password[^]

Have a look at this article too: Active Directory Object Navigator[^]
Share this answer
murugesan.velusamy 15-Jun-12 7:13am    
I am actually trying this under the domain.The website is running in different domain. tell me how to solve this issue.
On the DirectoryEntry constructor there is an override which contains the credentials used to access the directory. You need to make sure the user you provide here has permissions to access the relevant directory entries.

DirectoryEntry entry = new DirectoryEntry(path, username, password);

MSDN for directory entry:

DirectoryEntry Class[^]

MSDN for constructor

DirectoryEntry Constructor (String, String, String)[^]
Share this answer
murugesan.velusamy 15-Jun-12 7:15am    
where do i get these three parameters. will this work if the website runs in different domain.
Stephen Hewison 15-Jun-12 11:55am    
If you're accessing a directory from a machine not within the domain then it may be problematic although you can prefix the username with the domain e.g. mydomain\myuser

If that doesn't work, you may need to get your infrastructure manager to set up a trust between the machine executing the code and domain with directory. Although that said, where the code was developed is irrelevant. It only matters where you're running the code.

You already have the first parameter it's your Str variable. You'll have to get the username and password from the owner of the domain your application is trying to change.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900