locked
LDAP DirectoryEntry SearchResult returns data differently in Windows 8 than Win7 RRS feed

  • Question

  • User-533558460 posted
    DirectoryEntry DEConnection = new DirectoryEntry("LDAP://directory.mydomain.com/dc=mydomain,dc=com");
    DEConnection.AuthenticationType = AuthenticationTypes.None;
    DirectorySearcher DESearch = new DirectorySearcher();
    DESearch.SearchRoot = DEConnection;
    DESearch.Filter = "(uid=" + txtNetID.Text + ")";
    try
    {
    SearchResult result = DESearch.FindOne();
    
    if (result != null)
    {
    DirectoryEntry myDirectoryEntry = result.GetDirectoryEntry();
    txtFullName.Text = myDirectoryEntry.Properties["displayName"].Value.ToString();
    
    txtFullName.Text = result.Properties["displayName"][0].ToString();
    

    In Windows 7 the last two lines properly return a string containing the users displayName.

    In Windows 8 instead of a string value it returns System.Byte[].

    When examining the RESULT object during debugging the on Windows 8 the value is stored as a byte array of ascii values, where in Windows 7 it is stored as a string.

    As yet I've been unable to find a way to get the string value out in Windows 8, or find any documentation anywhere noting this difference.

    What is the easiest way to modify the code to work on both operating systems?

    Tuesday, February 19, 2013 11:22 PM

All replies

  • User-905528745 posted

    Having had my share of problems with some LDAP system the company uses (not AD), I have come across string values returned as byte arrays.  Being tired of trying to understand why this happens and the serious lack of support from the LDAP team (other than "hey, that's weird right?") I simply opted to add a check on the data type and translate whenever necessary.  If you get a byte array, translate using an encoder.  For AD I would try with UTF8, UTF16 (Unicode), ASCII or Windows-1251.  If you are unfamiliar with encoders, I guess it is time to learn.  Google up System.Text.Encoding.  It should be as simple as using the encoder's GetString() method.  Good luck.

    Wednesday, February 20, 2013 12:03 AM
  • User-533558460 posted

    Yeah that is what I ended up doing, just strange that it only became a problem with Windows 8 when querying our LDAP server.  The same application works on all the previous Windows operating systems it runs on.

    Friday, February 22, 2013 10:57 AM
  • User888677273 posted

    Was this ever fixed?

    Monday, April 22, 2013 2:43 PM
  • User-533558460 posted

    I fixed my application so it works, nothing has changed with the behavior of Windows 8.


    Here's my solution to the problem:

                DirectoryEntry DEConnection = new DirectoryEntry("LDAP://mydirectory.mydomain.com/..........");
                DEConnection.AuthenticationType = AuthenticationTypes.None;
                DirectorySearcher DESearch = new DirectorySearcher();
                DESearch.SearchRoot = DEConnection;
                DESearch.Filter = "(uid=" + txtNetID.Text + ")";
                try
                {
                    SearchResult result = DESearch.FindOne();
    
                    if (result != null)
                    {
                        if (result.Properties["displayName"][0].GetType().IsArray)
                        {
                            txtFullName.Text = System.Text.Encoding.UTF8.GetString((byte[])result.Properties["displayName"][0]);
                        }
                        else
                        {
                            txtFullName.Text = result.Properties["displayName"][0].ToString();
                        }
                    }
                }
    
     


     

    Monday, April 22, 2013 5:13 PM
  • Tuesday, April 30, 2013 6:27 PM