搜索LDAP并创建CSV文件

本文关键字:CSV 文件 创建 LDAP 搜索 | 更新日期: 2023-09-27 18:10:26

我编写了以下程序,该程序连接到两个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次。

搜索LDAP并创建CSV文件

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

1)首先,你创建了两次相同的搜索结果:
   SearchResultCollection absaUsers = ds.FindAll();
   SearchResultCollection srcUsers = ds.FindAll();

如果搜索器找到10个用户,那么这里有两个相同的10个用户集合。

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

3)你似乎缺少了某种限制/条件,只能从第二/内部结果集中挑选出那些以某种方式匹配外部/第一个结果集的元素。只要没有这样的条件,您将始终从第二个结果集中获得第一个结果集中的每个元素的所有结果=经典笛卡尔积。出于某种原因,您希望基于第一个结果集......中的某些内容,仅从第二个结果集中选择某些元素

您在下列位置错过了换行符:

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