简体   繁体   中英

C# Active Directory - Incorrect Username and Password but details are actually correct

Issue with Active Directory & C#. I'm getting an intermittent error stating that "The user name or password is incorrect.". However, I know the login details are correct as it works Locally in VS. It seemingly doesn't on the web server everytime.

Error:

The user name or password is incorrect.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DirectoryServices.DirectoryServicesCOMException: The user name or password is incorrect.

My code for storing the connection to the LDAP, bits hidden for security reasons:

    public static DirectoryEntry GetDirectoryEntryWithGc()
    {
        var de = new DirectoryEntry("GC://dc=TEXT,dc=TEXT,dc=TEXT");
        de.AuthenticationType = AuthenticationTypes.Secure;
        de.Username = "USERNAME";
        de.Password = "PASSWORD";
        return de;
    }

As mentioned, works Locally and is intermittent. Anyone any experience of this?

This is just a guess, but is there more than one domain in your AD forest? And are you including the domain name in "USERNAME" (ie "DOMAIN\USERNAME" )?

Using GC:// without specifying a server name can connect to a server from any of the domains in your forest. If you did not specify the domain name in your username, then it will assume the username is from the server's domain, which may not be the case.

If I'm correct in this, then you can solve the issue by including the domain name with the username:

de.Username = "DOMAIN\USERNAME";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM