Barnabeck
Barnabeck

Reputation: 481

LDAP Search for multiple names returns entries in a different order

I work with an LDAP filter that is dynamically built to get the thumnailPhotos of a series of Active Directory users and place them in a Repeater

string UserIDs = "(|(SamAccountName=SummarS)(SamAccountName=RuatG)(SamAccountName=ArtisaD))" <- is build in a loop operation 
string OnlyEnabled = "!(userAccountControl:1.2.840.113556.1.4.803:=2)";
dsSearcher.Filter = "(&(objectCategory=person)(objectClass=user)"+ UserIDs + "(" + OnlyEnabled + "))";
dsSearcher.PropertiesToLoad.Add("SamAccountName");
dsSearcher.PropertiesToLoad.Add("thumbnailPhoto");

using (var results = dsSearcher.FindAll())
{
    var t = new DataTable("ActiveDir");
    t.Columns.Add(new DataColumn("UserID", typeof(string)));
    t.Columns.Add(new DataColumn("data", typeof(byte[])));
    int i = 0;
    foreach (SearchResult searchResult in results)
    {
        Image SRC = RPT_Podium.Items[i].FindControl("ImgData") as Image;
        var bytes = searchResult.Properties["thumbnailPhoto"][0] as byte[];
        MemoryStream s = new MemoryStream(bytes);
        byte[] imageBytes = s.ToArray();
        string base64String = Convert.ToBase64String(imageBytes);
        string imageUrl = "data:image/jpg;base64," + base64String;
        SRC.ImageUrl = imageUrl;
        i = i + 1;
    }
}

To my surprise the returned results are NOT in the order of the AccountNames as appearing in the Search String. This forces me to reorder them somehow and I still can't think of the smartest way to achieve that


This is the solution I finally came up with:
string OU = "LDAP://DC=CORP,DC=ROOT,DC=INT";

string OnlyEnabled = "!(userAccountControl:1.2.840.113556.1.4.803:=2)";

DirectoryEntry startingPoint1 = new DirectoryEntry(OU);
DirectorySearcher dsSearcher = new DirectorySearcher(startingPoint1);
dsSearcher.Filter = "(&(objectCategory=person)(objectClass=user)"+ UserIDs + "(" + OnlyEnabled + "))";

dsSearcher.PropertiesToLoad.Add("SamAccountName");
dsSearcher.PropertiesToLoad.Add("thumbnailPhoto");

using (var results = dsSearcher.FindAll())
{
    foreach (SearchResult searchResult in results)
    {
        foreach(RepeaterItem ri in RPT_Podium.Items)
        {
            Label LBL = ri.FindControl("LBL_UserID") as Label;
            if(searchResult.Properties["SamAccountName"][0].ToString() == LBL.Text)
            {
                Image SRC = ri.FindControl("ImgData") as Image;
                var bytes = searchResult.Properties["thumbnailPhoto"][0] as byte[];
                MemoryStream s = new MemoryStream(bytes);
                byte[] imageBytes = s.ToArray();
                string base64String = Convert.ToBase64String(imageBytes);
                string imageUrl = "data:image/jpg;base64," + base64String;
                SRC.ImageUrl = imageUrl;
            }
        }
    }
}

Upvotes: 0

Views: 433

Answers (1)

jwilleke
jwilleke

Reputation: 11026

LDAP (the protocol) provides no guarantees as to the order of returned entries.

You Might be able to implement this server-side. Sorting the results of a DirectorySearcher query by DateTime

Or you would need to perform the sort on the client.

Upvotes: 2

Related Questions