1

I'm trying to update some AD accounts using C#. I have:

void UpdateADUser(string emailaddress)
{
        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();
            DirectorySearcher search = new DirectorySearcher(myLdapConnection);
            search.Filter = "(cn=" + emailaddress + ")";
            search.PropertiesToLoad.Add("title");
            SearchResult result = search.FindOne();

            if (result != null)
            {
               DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
               Response.Write("Current title   : " +
                                  entryToUpdate.Properties["title"][0].ToString());
            }

            else Response.Write("User not found!");
        }

        catch (Exception e)
        {
            Response.Write("Exception caught:\n\n" + e.ToString());
        }  
    }

    static DirectoryEntry createDirectoryEntry()
    {
        DirectoryEntry ldapConnection = new DirectoryEntry("leasing-vm1.**********.com");
        ldapConnection.Path = "LDAP://OU=leasing options,DC=leasing,DC=local";
        ldapConnection.AuthenticationType = AuthenticationTypes.None;
        ldapConnection.Username = "administrator";
        ldapConnection.Password = "D**********s";

        return ldapConnection;
    }  

I'm getting an error:

The specified domain either does not exist or could not be contacted.

Any help appreciated. One potential issue is that my development machine is not part of the domain concerned. Is that the problem?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ben Durkin
  • 429
  • 1
  • 6
  • 20
  • do you need a `DC=COM` as well.. also check out the following link if you have not checked it out before [C# and ActiveDirectory](http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C) as well as this link http://stackoverflow.com/questions/14813452/connect-to-active-directory-via-ldap `An Easier way to connect also would be to use PrincipalContext` I use it currently to avoid the LDAP Connection headaches – MethodMan Jan 15 '15 at 20:52

1 Answers1

0

If current machine is not joined to domain, you have to specify the domain/DC to connect in the LDAP path.

e.g. LDAP://leasing.local/OU=leasing options,DC=leasing,DC=local

baldpate
  • 1,707
  • 1
  • 14
  • 23