Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有C#DirectoryEntry类的多个LDAP连接_C#_Multithreading_Ldap_Directoryentry - Fatal编程技术网

具有C#DirectoryEntry类的多个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

我正在使用DirectoryEntry类进行LDAP身份验证。当我使用单个LDAP连接字符串时,它可以正常工作。但是,一旦我开始在多个线程上为多个LDAP连接字符串执行代码,它就会开始随机抛出身份验证异常,即使用户名和密码是正确的。 我正在使用以下代码

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的方法:

  • 使用DirectoryEntry类
  • 使用LdapConnection和NetworkCredential类
  • 使用PrincipalContext类
  • 我观察到的是,如果应用程序一次使用多个AD服务器,并同时打开与多个AD服务器的连接,则DirectoryEntry方法不起作用。对于有效凭据,它抛出“未授权访问”异常。我的猜测是,一些数据/变量是共享的,它试图根据不正确的AD服务器对用户进行身份验证。我研究了汇编代码,并验证了它使用静态变量。这可能是真正的原因,但不是100%确定。 如果应用程序需要同时打开到多个AD服务器的连接,我的解决方法是使用其他两种方法。在过去的六个月里,我用大约10个同步连接对它进行了测试,它的工作状况良好


    如果有人面临同样的问题,希望这能有所帮助。

    您的代码在哪一行引发异常?
    IsAuthenticated
    方法是否通过ASMX webservice公开?在“IsAuthenticated”函数的顶部有一个包装函数。该功能通过ASMXWeb服务公开。执行entry.RefreshCache()行时,捕获异常“登录失败:未知用户名或错误密码”。当我在JMeter的帮助下在多个线程(每个线程都有不同的LDAP连接字符串)上随机调用ASMX web服务时,请求会失败,出现上述异常。您可以显示用于调用
    IsAuthenticated
    的代码吗?我已经用调用上述方法的函数的代码更新了我的问题。