Mat*_*t M 6 .net directoryservices
首先,我看了大多数关于SO的问题,但似乎没有一个问题完全相同.这是一个类似的问题,但并不完全相同.在我的情况下,我正在创建一个PrincipalContext:
Dim pctx As PrincipalContext = New PrincipalContext(ContextType.Domain, fullyqualifieddomain, container, ADUserID, ADPassword)
If pctx.ValidateCredentials(userName, password) Then
Run Code Online (Sandbox Code Playgroud)
ADUserID是一个服务帐户.
此方法有效,但需要6-10秒.
我也尝试直接检索底层目录条目和绑定.这速度更快,可以在我的机器上运行(在域外),但不在Web服务器上(在域内).它在DirectoryEntry.NativeObject调用时失败.我不知道为什么.不幸的是,我处于这样一种情况,即唯一可行的方法是太慢而不可行.有没有办法加快速度?
提前致谢!
Pet*_*ter 12
请尝试下面的代码.它可能不会更快,但看它是否有效会很高兴.
用户名不应包含域名.对于域我的测试只使用短名称"DOMAIN",而不是DN或甚至完全合格(您的milage可能会有所不同).
添加对System.DirectoryServices.Protocols的引用.
using System.DirectoryServices.Protocols;
public static bool Authenticate(string username, string password, string domain)
{
try
{
//string userdn;
using (LdapConnection lconn = new LdapConnection(new LdapDirectoryIdentifier(domain)))
{
lconn.Bind(new System.Net.NetworkCredential(username, password, domain));
return true;
}
}
catch (LdapException e)
{
return false;
}
}
if (Authenticate("username", "password", "domain")) { }
Run Code Online (Sandbox Code Playgroud)