Move Object to an OU in Active Directory

2019-08-21 02:17发布

问题:

I want to move a computer object to another OU I am connected to another domain and I always getting an exception of type ComException "A referral was returned from the server" and the object never move!

        try
        {
            //I get the exception here
            computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"));
            computerObject.CommitChanges();
        }
        catch (InvalidOperationException inOp)
        {
            //log 
        }
        catch (COMException comEx)
        {
            //log 
        }

        //joinPath.Close();
        finally
        {
            computerObject.Close();
        }

for troubleshooting I changed the code a little bit but again it doesn't work.

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"),
                      "admin@test.com","somepassowrd",AuthenticationTypes.Secure));

the new exception is of type ComException "Logon failure: unknown user name or bad password." I checked that there exists an OU in the active directory and I have enough permissions.

I followed Microsoft docs here https://msdn.microsoft.com/en-us/library/ms180856(v=vs.90).aspx and many stackoverflow questions.

Update: I am running my application in one domain and making changes in another domain, it might be the cause of the problem

回答1:

I published my code in the same domain and it worked just fine



回答2:

You have an extra parenthesis in this method:

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"),"admin@test.com","somepassowrd",AuthenticationTypes.Secure));

it should be

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com","admin@test.com", "somepassowrd", AuthenticationTypes.Secure));

This would certainly explain the exception, I'm surprised you debugger didn't catch it, unless you are writing it freehand, or maybe you mis-typed it into your question?