C# 用户名或密码不正确-8009030C:LDAPPER:DSID-0C09079,注释:AcceptSecurityContext错误,数据52e,v3839

C# 用户名或密码不正确-8009030C:LDAPPER:DSID-0C09079,注释:AcceptSecurityContext错误,数据52e,v3839,c#,.net,authentication,active-directory,ldap,C#,.net,Authentication,Active Directory,Ldap,我正在尝试用以下代码连接AD VMADProfile _VMADProfile = new VMADProfile(); DirectoryEntry searchRoot = new DirectoryEntry("Test AD LDAP Path", "DomainName\\username", "password"); directorySearcher.Filter = "(&(objectCategory=user)(samAccountName=" + username

我正在尝试用以下代码连接AD

VMADProfile _VMADProfile = new VMADProfile();
DirectoryEntry searchRoot = new DirectoryEntry("Test AD LDAP Path", "DomainName\\username", "password");
directorySearcher.Filter = "(&(objectCategory=user)(samAccountName=" + username + "))";    
directorySearcher.PropertiesToLoad.Add("displayname");    
SearchResult one = directorySearcher.FindOne();
但它给了我以下的错误

 The user name or password is incorrect.
8009030C: LdapErr: DSID-0C090579, comment: AcceptSecurityContext error, data 52e, v3839
如果我直接登录,我可以使用相同的用户名和密码登录。
我尝试连接的广告与我运行代码的广告不同。如果我将测试广告更改为生产广告,同样的代码也可以正常工作,我认为这是关键:

我尝试连接的广告与我运行代码的广告不同

它可能试图在您的域上验证您的身份,而不是另一个域。确保LDAP路径包含要连接到其他域的服务器,而不仅仅是域的可分辨名称。服务器名称只能是域的DNS名称。它应该是这样的:

DirectoryEntry searchRoot = new DirectoryEntry("LDAP://otherdomain.com/DC=otherdomain,DC=com", "DomainName\\username", "password");

如果您只使用LDAP://DC=otherdomain,DC=com“,它将尝试连接到当前计算机首先加入的域。如果两个域之间存在信任关系,这实际上是可行的,但如果不存在信任关系,则需要专门为其指定要连接的服务器名称。

错误52e表示凭据无效,即密码不正确。正如我提到的密码正确,我可以使用相同的ADI登录服务器。我只是说说错误的含义。你能在密码中包含非ascii字符吗?是的,我已经有大写字母小写特殊字符和数字,那么这可能是字符集问题。拉丁文1对Unicode。