DirectoryEntry CommitChanges() is throwing Access Denied error










0















I am working on Active Directive Role modification module in my project and getting Access denied error while Adding/Removing the roles of users. I am Administrator of AD server.



I noticed one schenario which is given below.



1: New user added in AD without any role.



2: Applying any role for user -(Getting error Access denied)



3: If apply the Super User role then role added successfully.



Code is given below



 // Search for role in Adam.
DirectorySearcher directorySearcher = new DirectorySearcher(GetAdamSdsRoot());
directorySearcher.Filter = "(&(objectCategory=group)(cn=" + roleName + "))";
SearchResult searchResult = directorySearcher.FindOne();

// Remove user from role.
DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry();
directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");
directoryEntry.CommitChanges();









share|improve this question


























    0















    I am working on Active Directive Role modification module in my project and getting Access denied error while Adding/Removing the roles of users. I am Administrator of AD server.



    I noticed one schenario which is given below.



    1: New user added in AD without any role.



    2: Applying any role for user -(Getting error Access denied)



    3: If apply the Super User role then role added successfully.



    Code is given below



     // Search for role in Adam.
    DirectorySearcher directorySearcher = new DirectorySearcher(GetAdamSdsRoot());
    directorySearcher.Filter = "(&(objectCategory=group)(cn=" + roleName + "))";
    SearchResult searchResult = directorySearcher.FindOne();

    // Remove user from role.
    DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry();
    directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");
    directoryEntry.CommitChanges();









    share|improve this question
























      0












      0








      0








      I am working on Active Directive Role modification module in my project and getting Access denied error while Adding/Removing the roles of users. I am Administrator of AD server.



      I noticed one schenario which is given below.



      1: New user added in AD without any role.



      2: Applying any role for user -(Getting error Access denied)



      3: If apply the Super User role then role added successfully.



      Code is given below



       // Search for role in Adam.
      DirectorySearcher directorySearcher = new DirectorySearcher(GetAdamSdsRoot());
      directorySearcher.Filter = "(&(objectCategory=group)(cn=" + roleName + "))";
      SearchResult searchResult = directorySearcher.FindOne();

      // Remove user from role.
      DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry();
      directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");
      directoryEntry.CommitChanges();









      share|improve this question














      I am working on Active Directive Role modification module in my project and getting Access denied error while Adding/Removing the roles of users. I am Administrator of AD server.



      I noticed one schenario which is given below.



      1: New user added in AD without any role.



      2: Applying any role for user -(Getting error Access denied)



      3: If apply the Super User role then role added successfully.



      Code is given below



       // Search for role in Adam.
      DirectorySearcher directorySearcher = new DirectorySearcher(GetAdamSdsRoot());
      directorySearcher.Filter = "(&(objectCategory=group)(cn=" + roleName + "))";
      SearchResult searchResult = directorySearcher.FindOne();

      // Remove user from role.
      DirectoryEntry directoryEntry = searchResult.GetDirectoryEntry();
      directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");
      directoryEntry.CommitChanges();






      c# winforms active-directory






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 5:58









      Arun SinghArun Singh

      68




      68






















          1 Answer
          1






          active

          oldest

          votes


















          0














          For one, this isn't going to work:



          directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");


          The member attribute is a list of distinguishedName attributes of the members, not their SIDs. So you need to remove the user's distinguishedName, not the SID.



          But if that was the only problem, the exception would not be "Access denied".



          I know you said you are an administrator, but are you running this under your administrator credentials?






          share|improve this answer























          • Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

            – Arun Singh
            Nov 14 '18 at 8:25












          • Have you changed your code to use the distinguishedName of the user and not the SID?

            – Gabriel Luci
            Nov 14 '18 at 13:13











          • Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

            – Arun Singh
            Nov 15 '18 at 7:53











          • You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

            – Gabriel Luci
            Nov 15 '18 at 12:18











          • So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

            – Gabriel Luci
            Nov 15 '18 at 13:17










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274688%2fdirectoryentry-commitchanges-is-throwing-access-denied-error%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          For one, this isn't going to work:



          directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");


          The member attribute is a list of distinguishedName attributes of the members, not their SIDs. So you need to remove the user's distinguishedName, not the SID.



          But if that was the only problem, the exception would not be "Access denied".



          I know you said you are an administrator, but are you running this under your administrator credentials?






          share|improve this answer























          • Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

            – Arun Singh
            Nov 14 '18 at 8:25












          • Have you changed your code to use the distinguishedName of the user and not the SID?

            – Gabriel Luci
            Nov 14 '18 at 13:13











          • Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

            – Arun Singh
            Nov 15 '18 at 7:53











          • You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

            – Gabriel Luci
            Nov 15 '18 at 12:18











          • So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

            – Gabriel Luci
            Nov 15 '18 at 13:17















          0














          For one, this isn't going to work:



          directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");


          The member attribute is a list of distinguishedName attributes of the members, not their SIDs. So you need to remove the user's distinguishedName, not the SID.



          But if that was the only problem, the exception would not be "Access denied".



          I know you said you are an administrator, but are you running this under your administrator credentials?






          share|improve this answer























          • Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

            – Arun Singh
            Nov 14 '18 at 8:25












          • Have you changed your code to use the distinguishedName of the user and not the SID?

            – Gabriel Luci
            Nov 14 '18 at 13:13











          • Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

            – Arun Singh
            Nov 15 '18 at 7:53











          • You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

            – Gabriel Luci
            Nov 15 '18 at 12:18











          • So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

            – Gabriel Luci
            Nov 15 '18 at 13:17













          0












          0








          0







          For one, this isn't going to work:



          directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");


          The member attribute is a list of distinguishedName attributes of the members, not their SIDs. So you need to remove the user's distinguishedName, not the SID.



          But if that was the only problem, the exception would not be "Access denied".



          I know you said you are an administrator, but are you running this under your administrator credentials?






          share|improve this answer













          For one, this isn't going to work:



          directoryEntry.Properties["member"].Remove("<SID=" + GetActiveDirectoryUserSid(userName) + ">");


          The member attribute is a list of distinguishedName attributes of the members, not their SIDs. So you need to remove the user's distinguishedName, not the SID.



          But if that was the only problem, the exception would not be "Access denied".



          I know you said you are an administrator, but are you running this under your administrator credentials?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 14:14









          Gabriel LuciGabriel Luci

          10.8k11424




          10.8k11424












          • Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

            – Arun Singh
            Nov 14 '18 at 8:25












          • Have you changed your code to use the distinguishedName of the user and not the SID?

            – Gabriel Luci
            Nov 14 '18 at 13:13











          • Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

            – Arun Singh
            Nov 15 '18 at 7:53











          • You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

            – Gabriel Luci
            Nov 15 '18 at 12:18











          • So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

            – Gabriel Luci
            Nov 15 '18 at 13:17

















          • Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

            – Arun Singh
            Nov 14 '18 at 8:25












          • Have you changed your code to use the distinguishedName of the user and not the SID?

            – Gabriel Luci
            Nov 14 '18 at 13:13











          • Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

            – Arun Singh
            Nov 15 '18 at 7:53











          • You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

            – Gabriel Luci
            Nov 15 '18 at 12:18











          • So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

            – Gabriel Luci
            Nov 15 '18 at 13:17
















          Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

          – Arun Singh
          Nov 14 '18 at 8:25






          Yes, I am running under the administrator account. Also noticed one more things that is given below. 1: There is 9 role on server. two is parent role and 7 is child folder role. If I assigned parent role then it work and if assigned child role then showing error. 2: If I assigned parent role and again updating child role then not getting access denied iissue.

          – Arun Singh
          Nov 14 '18 at 8:25














          Have you changed your code to use the distinguishedName of the user and not the SID?

          – Gabriel Luci
          Nov 14 '18 at 13:13





          Have you changed your code to use the distinguishedName of the user and not the SID?

          – Gabriel Luci
          Nov 14 '18 at 13:13













          Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

          – Arun Singh
          Nov 15 '18 at 7:53





          Yes, I changed but getting diff error : "information: An invalid dn syntax has been specified.". My DN is "CN=singh Arunkumar,OU=Consultants,OU=Users,OU=SE,OU=CRFREE,DC=CRFREE,DC=com"/ Code line : directoryEntry.Properties["member"].Remove("<distinguishedName=" + GetActiveDirectoryUserDistinguishedName(userName) + ">");

          – Arun Singh
          Nov 15 '18 at 7:53













          You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

          – Gabriel Luci
          Nov 15 '18 at 12:18





          You don't need the "<distinguishedName=" . You just pass the DN to the Remove method.

          – Gabriel Luci
          Nov 15 '18 at 12:18













          So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

          – Gabriel Luci
          Nov 15 '18 at 13:17





          So in your case: directoryEntry.Properties["member"].Remove(GetActiveDirectoryUserDistinguishedName(userName))

          – Gabriel Luci
          Nov 15 '18 at 13:17

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274688%2fdirectoryentry-commitchanges-is-throwing-access-denied-error%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo