Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am making a windows application that sync the source data to Active Directory.

This application works like this.

1. Choose Source Data(Department, User)

2. Mapping user or department attributes from source data

3. When Application service is run, it create groups and users in Active Directory

4. And also it sets the attributes to users and groups.

When I try to set group or user attributes(properties), it throws exception message like this.

The directory
 service cannot perform the requested operation on the RDN attribute of an object.


I tried to solve it, but it`s really hard to me because I`m not good at Active directory...

Code is below, Please share your knowledge.

What I have tried:

void CreateADUser(string ppk,string pk,DataRow row)
{
    string pass = GetPass(pk,row,LogSections.AD);
    DirectoryEntry addept = adm.FindOU(ppk);
    string sOU = adm.GetPrincipalPath(addept);
    var aduser = adm.CreateNewUser(sOU, pk, pass, pk, null, null, adm.sDomain);
    SetAdUserProperties(pk, pass, row);    
    MoveUser(ppk,pk);
}



void SetAdUserProperties(string pk,string pass,DataRow row)
{
    if (row == null) return;
    List<ADMapping> MappingPatterns = GetAdMappings(Words.User,false);
    var colnames = Tool.GetColNames(row);
    var aduser = adm.GetUser(pk);
    var de=aduser.GetUnderlyingObject() as DirectoryEntry;
    foreach (var ADMap in MappingPatterns)
    {
        string val = ADMap.Mapping;
        val=Util.ReplaceColPattern(val, row);
        SetProperty(de, ADMap.CN, val);
    }
    if (!string.IsNullOrWhiteSpace(pass))
    {
       var UserPkColumn = AppConfigHelper.GetAppString(Words.SourceUserPKColumn);
       UserPkColumn = Util.GetActualColName(UserPkColumn);
       aduser.SetPassword(pass);
       QueryHelper.Update(QueryHelper.ConnectionString, Words.ShadowUserTable
                    ,new SqlParameter[] { new SqlParameter("@passwd", pass) }
                    , new SqlParameter("@"+UserPkColumn,pk));
    }

     aduser.Save();
}

public void SetProperty(DirectoryEntry oDE, string sPropertyName, object sPropertyValue)
{
    if (sPropertyValue != null && !string.IsNullOrWhiteSpace(sPropertyValue.ToString()))
    {
        if (oDE.Properties.Contains(sPropertyName))
        {
            oDE.Properties[sPropertyName].Value = sPropertyValue;
        }
        else
        {
            oDE.Properties[sPropertyName].Add(sPropertyValue);
        }
        try
        {
            oDE.CommitChanges(); //exception here.
            oDE.Close();
         }
        catch (Exception)
        {   
        }
     }
}
Posted

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