C# 搜索LDAP并创建CSV文件

C# 搜索LDAP并创建CSV文件,c#,ldap,C#,Ldap,我编写了以下程序,它连接到两个LDAP存储,比较属性,并根据结果创建一个新的csv文件。不过我遇到了一个问题 代码如下: //Define LDAP Connection string username = "****"; string password = "*****"; string domain = "LDAP://****"; //Define LDAP Connection strin

我编写了以下程序,它连接到两个LDAP存储,比较属性,并根据结果创建一个新的csv文件。不过我遇到了一个问题

代码如下:

        //Define LDAP Connection
        string username = "****";
        string password = "*****";
        string domain = "LDAP://****";

        //Define LDAP Connection 
        string ABSAusername = "****";
        string ABSApassword = "****";
        string ABSAdomain = "LDAP://****";

        //Create Directory Searcher
        DirectoryEntry ldapConnection = new DirectoryEntry(domain,username,password);
        ldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
        DirectorySearcher ds = new DirectorySearcher(ldapConnection);
        ds.Filter = "((EmploymentStatus=0))";
        ds.SearchScope = System.DirectoryServices.SearchScope.Subtree;

        //Create Directory Searcher
        DirectoryEntry ABSAldapConnection = new DirectoryEntry(ABSAdomain, ABSAusername, ABSApassword);
        ABSAldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
        DirectorySearcher ABSAds = new DirectorySearcher(ABSAldapConnection);
        ABSAds.Filter = "((&(EmploymentStatus=3)(EmploymentStatusDescription=Active))";
        ABSAds.SearchScope = System.DirectoryServices.SearchScope.Subtree;

        ds.PropertiesToLoad.Add("cn");
        ds.PropertiesToLoad.Add ("uid");
        ds.PropertiesToLoad.Add("sn");
        ds.PropertiesToLoad.Add("PersonnelAreaDesc");
        ds.PropertiesToLoad.Add("JobcodeID");
        ds.PropertiesToLoad.Add("CostCentreID");
        ds.PropertiesToLoad.Add("CostCentreDescription");
        ds.PropertiesToLoad.Add ("givenName");
        ds.PropertiesToLoad.Add ("EmploymentStatus");
        ds.PropertiesToLoad.Add("EmploymentStatusDescription");

        ABSAds.PropertiesToLoad.Add("uid");
        ABSAds.PropertiesToLoad.Add("EmploymentStatus");

        ABSAds.Sort = new SortOption("uid", SortDirection.Ascending);
        ds.Sort = new SortOption("cn", SortDirection.Ascending);

        SearchResultCollection absaUsers = ds.FindAll();
        SearchResultCollection srcUsers = ds.FindAll();

        sw.WriteLine("Action" + "," + "uid" + "," + "Business Area" + "," + "employeeNumber" + "," + "First Name" + "," + "Last Name" + "," + "JobCodeID" + "," + "costCentreID" + "," + "costCentreDescription" + "," + "FullName" + "," + "EmploymentStatus" + "," + "EmploymentStatusDescription" );
        sw.WriteLine("");

        foreach (SearchResult users in srcUsers)
        {


            string cn = users.Properties["cn"][0].ToString();
            string sn = users.Properties["sn"][0].ToString();
            string userID = users.Properties["uid"][0].ToString();
            string description = users.Properties["PersonnelAreaDesc"][0].ToString();
            // string jobCodeID = users.Properties["JobcodeID"][1].ToString();
            string CostCentreID = users.Properties["costCentreID"][0].ToString();
            string CostCentreDescription = users.Properties["CostCentreDescription"][0].ToString();
            string givenName = users.Properties["givenName"][0].ToString();
            string employmentStatus = users.Properties["EmploymentStatus"][0].ToString();
            string EmploymentStatusDescription = users.Properties["EmploymentStatusDescription"][0].ToString();

            foreach (SearchResult absaUser in absaUsers)
            {
                string absaUID = absaUser.Properties["uid"][0].ToString();
                string absaEmploymentStatus = absaUser.Properties["EmploymentStatus"][0].ToString();

                if (cn == absaUID)
                {
                    if (absaEmploymentStatus == "3")
                    {

                        sw.WriteLine(cn);
                    }
                }
            }
        }


        sw.Flush();
        sw.Close();
        sw.Dispose();
    }
}
我创建了两个foreach循环,在第一个循环中我将变量分配给字符串,在第二个foreach循环中我与IF语句进行比较。我想做的是:如果一个LDAP中的uid与另一个LDAP中的uid相等,并且如果第一个LDAP中的用户状态为0,但第二个LDAP中的用户状态为3:那么我想从第一个LDAP打印出符合该条件的用户

如果你仔细看我的代码,我做错什么了吗?该程序目前的输出约为10个用户,每个用户至少复制100次


提前感谢。

您错过了以下地点的休息时间:

if (cn == absaUID && absaEmploymentStatus == "3")
{
    sw.WriteLine(cn);
    break;
}

这个代码有几个明显的错误

1) 首先,您将创建两次相同的搜索结果:

   SearchResultCollection absaUsers = ds.FindAll();
   SearchResultCollection srcUsers = ds.FindAll();
因此,如果搜索者找到10个用户,那么这里有两个相同10个用户的集合

2) 然后使用嵌套的foreach循环,基本上一个接一个地遍历第一个集合中的所有结果,并为每个条目枚举整个第二个集合-基本上在两个集合之间进行笛卡尔积。所以这就是为什么最终会有10 x 10=100个用户


3) 您似乎缺少某种限制/条件,无法从第二个/内部结果集中仅选择以某种方式与外部/第一个结果集匹配的元素。只要你没有这样的条件,你总是会从第二个结果集中得到第一个结果集的每个元素的所有结果=一个经典的笛卡尔积。不知何故,您只希望根据第一个结果集中的某些内容,从第二个结果集中选择某些元素

你知道哪些基本上已经完成了所有这些吗?这只适用于Win2003/2008服务器。我在我的桌面上运行这个。那是我的一个愚蠢的错误。好的,我已经编辑了这些集合,现在它是两个独立的集合。如果我使用嵌套循环,结果还会一样吗?@Trishen:您似乎缺少某种“连接”条件-如果您没有任何限制,那么是的-您将从第二个结果集中获得每个元素的所有元素frmo第一个-一个经典的笛卡尔积(您将得到m乘以n的结果)你知道我会怎么做吗?