使用C#中的AD凭据登录

vml*_*l19 0 c# asp.net active-directory

我使用以下代码来验证AD用户

string strLDAP = "LDAP://dc=ADServerIP/cn=Users,DC=Domain;
DirectoryEntry entry = new DirectoryEntry(strLDAP, usr, pwd);
object nativeObject = entry.NativeObject;
return true;
Run Code Online (Sandbox Code Playgroud)

执行时我收到以下异常

object nativeObject = entry.NativeObject;
Run Code Online (Sandbox Code Playgroud)

System.Runtime.InteropServices.COMException(0x80005000): 位于System.DirectoryServices.DirectoryEntry.get_NativeObject() 的System.DirectoryServices.DirectoryEntry.Bind()的
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
中的未知错误(0x80005000
)

相同的代码适用于另一台AD服务器.可能是什么问题?

mar*_*c_s 7

您是在使用.NET 3.5还是更新?如果是这样,您可以使用System.DirectoryServices.AccountManagement命名空间并轻松验证您的凭据:

// create a "principal context" - e.g. your domain (could be machine, too)
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", usr, pwd))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}
Run Code Online (Sandbox Code Playgroud)

它很简单,可靠,它是你的100%C#托管代码 - 你还能要求什么?:-)

在这里阅读所有相关内容: