Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please assist: I wrote the below code to move an account in AD from one OU to another OU, I am getting a "Success" resultCode however the move is not reflecting in AD, it doesn't seem to be working, can anyone advise me if i am doing some wrongin the code or if there is a way to see why i am getting a success even though it's not actually working.

What I have tried:

<pre>string status = string.Empty;
string domain = "domain-secure.corp";
string userDistinguishedName = "CN=TestUser,OU=Users,OU=PreStaging,DC=domain-secure,DC=corp";
string newDistinguishedName = "OU=Disabled Users,OU=Disabled,DC=domain-secure,DC=corp";
LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(domain, 636));
connection.SessionOptions.SecureSocketLayer = true;
connection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
connection.Credential = new NetworkCredential(AdminUsername, AdminPassword);
connection.AuthType = AuthType.Basic;
connection.Bind();

 

string newDistinguishedName = $"CN={cn},{terminationsDistinguishedName}";
ModifyDNRequest request = new ModifyDNRequest(userDistinguishedName, newDistinguishedName, $"CN={cn}");
request.DeleteOldRdn = true;
ModifyDNResponse response = (ModifyDNResponse)connection.SendRequest(request);

 

if (response.ResultCode == ResultCode.Success)
{
    status = "Success";
}
else
{
    status = $"Failed Moving User : {response.ResultCode}";
}
return status;
Posted
Updated 11-Jul-23 2:10am

In your code, the 'userDistinguishedName' and 'newDistinguishedName' variables should contain the full distinguished name of the user and the destination OU. Tthe values you provided looks to be incomplete or incorrect. Make sure that you have the correct distinguished names for both the user and the target OU.

The code you provided is using the 'ModifyDNRequest' class, which is used to modify the RDN of an entry. To move an account from one OU to another, you need to perform a different LDAP operation called "LDAP Modify" that involves modifying the 'distinguishedName' attribute of the user's entry to the new OU's distinguished name. a Full description and sample code is to be found at Modifying Entry Properties[^]

Your code should then look similar to -
C#
string domain = "domain-secure.corp";
string userDistinguishedName = "CN=TestUser,OU=Users,OU=PreStaging,DC=domain-secure,DC=corp";
string newParentDistinguishedName = "OU=Disabled Users,OU=Disabled,DC=domain-secure,DC=corp";
string newUserDistinguishedName = $"CN=TestUser,{newParentDistinguishedName}";

using (LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(domain, 636)))
{
    connection.SessionOptions.SecureSocketLayer = true;
    connection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
    connection.Credential = new NetworkCredential(AdminUsername, AdminPassword);
    connection.AuthType = AuthType.Basic;
    connection.Bind();

    ModifyDNRequest request = new ModifyDNRequest(userDistinguishedName, newUserDistinguishedName, newParentDistinguishedName);
    request.DeleteOldRdn = true;

    try
    {
        ModifyDNResponse response = (ModifyDNResponse)connection.SendRequest(request);

        if (response.ResultCode == ResultCode.Success)
        {
            return "Success";
        }
        else
        {
            return $"Failed Moving User: {response.ResultCode}";
        }
    }
    catch (Exception ex)
    {
        return $"Error Moving User: {ex.Message}";
    }
}
 
Share this answer
 
Comments
Ismail Bhyat 11-Jul-23 8:10am    
Thanks for your assistance, I tried using the way you suggested in the link you provided, however this does not work as DistinguishedName is a readonly attribute.
I tried using the way you suggested in the link you provided, however this does not work as DistinguishedName is a readonly attribute.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900