开发者

Search LDAP and create CSV File

I have written the following program which connects to two LDAP stores, compares the attributes and based on the outcome, creates a new csv file. I have run into a problem though.

Here is the code:

        //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();
    }
}

I created two foreach loops, in the first loop i assigned variables to strings, and in the second foreach loop i do a comparison with an IF statement. What i want to do is: if the uid in one LDAP is equal to the uid in the other ldap, and if the status of the user in the 1st ldap = 0 but the status of the user in the 2nd ldap = 3: then i want to print out the users that match that criteria from the 1st ldap.

If you look through my code, am i doing somthing wrong? The outpu开发者_运维百科t of the program currently is about 10 users that are duplicated atleast 100 times each.

Thanks in advance.


There are several things obviously wrong with this code....

1) First of all, you're creating the same search result twice:

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

So if that searcher finds 10 users, you have two collections of the same 10 users here.

2) You then used nested foreach loops, so you basically go through all results from the first collection, one by one, and for each of those entries, you enumerate the entire second collection - basically doing a cartesian product between the two collections. So that's why you end up with 10 x 10 = 100 users in the end.....

3) you seem to be missing some kind of a restriction/condition to pick out only those elements from your second/inner result set that match the outer/first result set in some way. As long as you don't have such a condition in place, you'll always get all the results from the second result set for each element of the first result set = a classic cartesian product. Somehow, you want to select only certain elements from the second set, based on something from the first result set......


You have missed break in the following place:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜