none
When I try to set Active Directory user properties, throw exception.... RRS feed

  • Question

  • It`s a good day to die....

    I`m trying to make & modify windows application about AD group and users.

    This application works like this.

    Users choose property and mapping it from source data(DB or Excel files...)

    And press sync start button, that properties put into the AD User. 

    But When I set AD user`s properties, it throws exception like this in DirectoryEntry.CommitChanges(); block.

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

    Sadly I`m a newbie of Active Directory, I don`t know how to solve it... Please help me.

    Code is below.

    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)
            {
    
            }
    
         }
    
    
      }

    Friday, March 3, 2017 6:22 AM

Answers

  • Hi RydenChoi,

    Based on my search, you missed something in your code.

    If we want to commit cached changes, set UsePropertyCache property to true and call the CommitChanges method. To update values in the cache after changes to the directory are made, call the RefreshCache method.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 8, 2017 8:27 AM
    Moderator

All replies

  • Hi RydenChoi,

    Thank you for posting here.

    For your question, if you want to set the name of sProprttyName, Naming attributes (RDN attributes like CN and OU) need to be changed using the Rename method.

    You could use DirectoryEntry.Rename Method (String) instead.

    Please try the following code.

    oDE.Rename("sPropertyName=sPropertyValue");

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Christopher84 Monday, March 6, 2017 12:06 PM
    Monday, March 6, 2017 7:52 AM
    Moderator
  • Thank you for reply, Wendy Zang.

    I tried to use that code, but it also throw a exception.

    Exception message is different.

    There is a naming violation.
    I`m trying to find reason but it`s really hard... How can I handle it?? 



    • Edited by RydenChoi Tuesday, March 7, 2017 12:56 AM
    Tuesday, March 7, 2017 12:55 AM
  • Hi RydenChoi,

    Thank you for feedback.

    For the example of Directory.Rename Method (String), you could refer to the link.

    You could use the "sPropertyName" as it is an RDN attribute ID. It is essentially an

    alias for the actual RDN. For AD, the RDN attributes are CN, OU and DC. DC

    is used on domainDNS objects, OU is used on OUs and CN is used on everything else.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 7, 2017 2:01 AM
    Moderator
  • Hi Wendy Zang.

    Thank you for your reply and explanation.

    In my code,  SetProperty method

    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.Rename(oDE.Name); // Add this code

    oDE.CommitChanges(); //exception here. oDE.Close(); } catch (Exception) { } } }

    oDE.Path is like this : "LDAP://ycdev.neo/CN=pys1,OU=ceo,OU=management,OU=neoplus,OU=ROOTOU,DC=ycdev,DC=neo"

    oDE.Name is "CN=pys1", so I added oDE.Rename(oDE.Name);

    and then, No exception occurs, it seems works well.

    But When I checked Attribute editor in Active Directory, it`s not changed.

    Is that wrong??

    Tuesday, March 7, 2017 8:09 AM
  • Hi RydenChoi,

    Based on my search, you missed something in your code.

    If we want to commit cached changes, set UsePropertyCache property to true and call the CommitChanges method. To update values in the cache after changes to the directory are made, call the RefreshCache method.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 8, 2017 8:27 AM
    Moderator
  • Hi Wendy Zang.

    Finally you found it!

    I read your reply and then edited the code. Mapped properties were applied to Active directory.

    I really appreciate with your advice. You saved my time!!

    Thank you!! :)

    Thursday, March 9, 2017 8:59 AM
  • Hi RydenChoi,

    I am glad the reply would be helpful. Please mark the useful reply as answer.

    This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 9, 2017 9:03 AM
    Moderator