grayson
grayson

Reputation: 1037

ASP.NET LDAP SearchResults Properties returning Byte Array

I am using DirectorySearcher to try and get a list of users in AD to sync them with my App and have copied code from various SO sources, however I am not getting any property values. I'm using the following code:

        DirectorySearcher search = new DirectorySearcher();
        SearchResultCollection results = null;
        string sDefaultOU = "LDAP://...";
        DirectoryEntry de = new DirectoryEntry(sDefaultOU);
        string userName = "DonaldDuck";
        search = new DirectorySearcher
        {
            SearchRoot = de,
            PropertiesToLoad = { "displayname", "sAMAccountName"}, 
            Filter = "(sAMAccountName=" + userName + ")"    
        };
        results = search.FindAll();

        foreach (SearchResult result in results)
        {
            String name; 
            if (result.Properties["sAMAccountName"].Count > 0)
            {
                name = result.Properties["sAMAccountName"][0].ToString();
            }    
        }

However, instead of name being equal to to "DonaldDuck", it will be "Byte[10]" or Byte[x] where x is the length.

Can anyone see what I am doing wrong.

If I add a filter it returns one user, so I am pretty sure the code is working in terms of searching

Upvotes: 3

Views: 2706

Answers (2)

Alan
Alan

Reputation: 26

instead of

foreach (SearchResult result in results)
    {
        String name; 
        if (result.Properties["sAMAccountName"].Count > 0)
        {
            name = result.Properties["sAMAccountName"][0].ToString();
        }    
    }

try  

foreach (SearchResult result in results)
    {
        String name; 
        if (result.Properties["sAMAccountName"].Count > 0)
        {
            var thisDE=result.GetDirectoryEntry();
            name = thisDE.Properties["sAMAccountName"].Value.ToString();
        }    
    }

EDIT: Example I use this helper method to search my domain (when userDomainAndName="DOMAIN\UserName") but you should be able to tweak it for what you want.

 public static DirectoryEntry GetUserDirectoryEntryFromCurrentDomain(string userDomainAndName)
    {
        var Split = userDomainAndName.Split(@"\\".ToCharArray());

        var DomainNetBiosNAme = Split[0];
        var UserName = Split[1];

        var QueryString = $"(&(objectCategory=person)(objectClass=user)(sAMAccountName={UserName}))";

        DirectoryEntry rootDSE = GetDirectoryObject(
            "LDAP://" + DomainNetBiosNAme + "/rootDSE");

        string domain = "LDAP://" + (string)rootDSE.Properties[
            "defaultNamingContext"][0];

        var Searcher = new DirectorySearcher(new DirectoryEntry(domain), QueryString);
        var Result = Searcher.FindOne();

        var tReturn = Result.GetDirectoryEntry();
        return tReturn; 

    }

then to get my users PrimarySMTP address (for example)..

var TheUsersDirectoryEntry=GetUserDirectoryEntryFromCurrentDomain(userDomainAndName);
var TheUsersPrimarySMTP=TheUsersDirectoryEntry.Properties["mail"].Value.ToString();

Upvotes: 1

StfBln
StfBln

Reputation: 1157

Apparently this issue has been faced by others: LDAP DirectoryEntry SearchResult returns data differently in Windows 8 than Win7

AD is using LDAPv3 encoding the values using UTF8, the solution mentioned in the link above might work for you:

if (result.Properties["sAMAccountName"][0].GetType().IsArray)
{
    name = System.Text.Encoding.UTF8.GetString((byte[])result.Properties["sAMAccountName"][0]);
}
else
{
    name = result.Properties["sAMAccountName"][0].ToString();
}

Upvotes: 7

Related Questions