Question

Ok so i'm trying to pull a user from active directory into a DirectoryUser object and if I type it like this, it works fine:

DirectoryEntry user = new DirectoryEntry(@"LDAP://CN=Name, OU=Department, OU=Group, DC=Domain1, DC=Domain2");
user.Properties["thumbnailPhoto"].Clear();

But I need the values to be able to be changed, so I tried a formatted string:

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);
DirectoryEntry user = new DirectoryEntry(ldap);
user.Properties["thumbnailPhoto"].Clear();

But this causes an error "There is no such object on the server"

The constructor takes a string, and I pass in the same exact values that i was using when i wrote it literally, why is it working one way an not the other?!

EDIT: I just wanted to add that I double checked in several different ways to verify that the built string was coming out identical to the hard coded string and it is. I ran through debugger and checked the user.path value to verify that the strings were being stored exactly the same. Everything so far is identical, but one works and the other doesn't!

UPDATE: I noticed that if I hard code directly to a string variable:

string ldap = @"Jeremy Stafford", "IT", "QGT", "QGT", "Local";
DirectoryEntry user = new DirectoryEntry(ldap);

This works just fine. This leads me to believe that there may be a problem with the string "type". Maybe if I could convert the built string back to a primitive string (or rather a value type versus the reference type), it would work? But I have no Idea how to do that. Any ideas?

UPDATE: I ran a semantic test. Here is the code I used:

string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);                
                var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
                string message;

                if (ldapFormatted.Equals(ldapHardCoded))
                {
                    message = "They're the same value\n";
                }
                else
                {
                    message = "Strings are not the same value\n";
                }

                if (ldapFormatted.GetType() == ldapHardCoded.GetType())
                {
                   message += "They are the same type";
                }
                else
                {
                    message += "They are not the same type";
                }
                message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
                MessageBox.Show(message);

And here was the result:

enter image description here

Was it helpful?

Solution

I tried your code and filled the format variables and it works for me as you can see in the screenshot.

  string cn = "Jeremy Stafford";
  String group = "IT";
  string ou = "QGT";
  String domain1 = "QGT";
  string domain2 = "Local";
  string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);

  var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
  string message;

  if (ldapFormatted.Equals(ldapHardCoded))
  {
    message = "They're the same value\n";
  }
  else
  {
    message = "Strings are not the same value\n";
  }

  if (ldapFormatted.GetType() == ldapHardCoded.GetType())
  {
    message += "They are the same type";
  }
  else
  {
    message += "They are not the same type";
  }
  message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
  MessageBox.Show(message);

Message

You should really check whats in your input variables. Maybe you have some invisible chars in it. Convert your input variables into byte arrays and print out the array to get more insight what you have in there.

OTHER TIPS

I believe somethign wrong with a hardcoded string, since if you copy hard coded values to the variables cn, group, ... you got working test. So please share a code which initializes a variables cn, group, ou, domain1, domain2.

Test code below works fine.

 string cn = "Jeremy Stafford";
 string group = "IT";
 string ou = "QGT";
 string domain1 = "QGT";
 string domain2 = "Local";

 string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                             cn, group, ou, domain1, domain2);    
 string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                                      cn, group, ou, domain1, domain2);                
 var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";

 bool bothTheSame = ldapFormatted.Equals(ldapHardCoded) 
                    && 
                    ldapFormatted.GetType() == ldapHardCoded.GetType();

hmm, why don't you try hard coding the values in string.format, if that works you have something wrong with your variables.

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", "Name", "Department", "Group", "Domain1", "Domain2");

Other than that, I don't think why it wouldn't work with hard coding and without, they both are strings, afterall.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top