具有C#DirectoryEntry类的多个LDAP连接
我正在使用DirectoryEntry类进行LDAP身份验证。当我使用单个LDAP连接字符串时,它可以正常工作。但是,一旦我开始在多个线程上为多个LDAP连接字符串执行代码,它就会开始随机抛出身份验证异常,即使用户名和密码是正确的。 我正在使用以下代码具有C#DirectoryEntry类的多个LDAP连接,c#,multithreading,ldap,directoryentry,C#,Multithreading,Ldap,Directoryentry,我正在使用DirectoryEntry类进行LDAP身份验证。当我使用单个LDAP连接字符串时,它可以正常工作。但是,一旦我开始在多个线程上为多个LDAP连接字符串执行代码,它就会开始随机抛出身份验证异常,即使用户名和密码是正确的。 我正在使用以下代码 public bool IsAuthenticated(string path, string domain, string group, string username, string pwd) { string domainAndUs
public bool IsAuthenticated(string path, string domain, string group, string username, string pwd)
{
string domainAndUsername = domain + @"\" + username;
LogManager.Application.DebugFormat("Inside IsAuthenticated for User {0} from Domain {1} and Group {2} of Path {3} ", username, domain, group, path);
try
{
using (DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername, pwd))
{
entry.AuthenticationType = AuthenticationTypes.Secure;
entry.RefreshCache();
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
using (DirectorySearcher search = new DirectorySearcher(entry))
{
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
LogManager.Application.ErrorLogFormat("User {0} is not available in Domain {1}", username, domain);
return false;
}
}
LogManager.Application.DebugFormat("User {0} is available in Domain {1}", username, domain);
return true;
}
}
catch (Exception ex)
{
LogManager.Application.ErrorLogFormat("Exception occured while authenticating user {0} : Error {1} ", username, ex.Message);
return false;
}
}
此函数通过ASMX web服务公开。此web服务由多个用户同时执行。每个用户提供路径(LDAP://{IP}/DC={Domain},DC=COM)
、域和凭据。因此,同时为多个LDAP连接执行代码
更新:
这是调用上述函数的ASMX web服务函数:
public class ValidateUserService : System.Web.Services.WebService
{
[WebMethod]
public Models.AuthenticationToken IsUserAuthenticated(string username, string password, string partnerName)
{
string path;
string group;
string domain;
// Internal Code to pull the domain name, group and path from the db with help of partnerName.
//each partner will have different path (LDAP conenction string) and domain.
bool isAuthenticated = IsAuthenticated(path, domain, group, username, password);
}
}
我所观察到的是,当来自不同广告的多个用户尝试执行此代码时,它会随机抛出身份验证错误。
正如您所看到的,代码没有任何静态变量。因此,对于每个调用,它都会创建DirectoryEntry
的新实例。因此,在更高的级别上,此设置代码应该与多个LDAP连接一起工作
话虽如此,有人见过这种行为吗?在内部,.net framework是否跨多个DirectoryEntry
实例共享数据?单个进程可以同时具有多个LDAP连接吗?
非常感谢您的帮助、建议或指点。我已经找到了解决该问题的方法,如果其他人遇到同样的问题,请将其发布。我尝试了以下使用AD/LDAP的方法:
如果有人面临同样的问题,希望这能有所帮助。您的代码在哪一行引发异常?
IsAuthenticated
方法是否通过ASMX webservice公开?在“IsAuthenticated”函数的顶部有一个包装函数。该功能通过ASMXWeb服务公开。执行entry.RefreshCache()行时,捕获异常“登录失败:未知用户名或错误密码”。当我在JMeter的帮助下在多个线程(每个线程都有不同的LDAP连接字符串)上随机调用ASMX web服务时,请求会失败,出现上述异常。您可以显示用于调用IsAuthenticated
的代码吗?我已经用调用上述方法的函数的代码更新了我的问题。