C# ActiveDirectory get PrincipalContext with “Rights”












0















I have a small web server running ASP.NET MVC on it. The Server is running with User "abc" but the User "abc" do not have rights for "changes" in ActiveDirectory.



So I have to pass the user login in the PrincipalContext with.



using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
{

GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

theGroup.Save();

}


the Code does work. But I do not like to transfair a Password from Methode to Methode...
=> on MVC I have a SSO and the Server knows me



System.Web.HttpContext.Current.User.Identity


It is possible to Use this Information?



new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???


Or MUST I give the password. And how to best pass from view to this method.



thanks










share|improve this question



























    0















    I have a small web server running ASP.NET MVC on it. The Server is running with User "abc" but the User "abc" do not have rights for "changes" in ActiveDirectory.



    So I have to pass the user login in the PrincipalContext with.



    using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
    {

    GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

    theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

    theGroup.Save();

    }


    the Code does work. But I do not like to transfair a Password from Methode to Methode...
    => on MVC I have a SSO and the Server knows me



    System.Web.HttpContext.Current.User.Identity


    It is possible to Use this Information?



    new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???


    Or MUST I give the password. And how to best pass from view to this method.



    thanks










    share|improve this question

























      0












      0








      0








      I have a small web server running ASP.NET MVC on it. The Server is running with User "abc" but the User "abc" do not have rights for "changes" in ActiveDirectory.



      So I have to pass the user login in the PrincipalContext with.



      using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
      {

      GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

      theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

      theGroup.Save();

      }


      the Code does work. But I do not like to transfair a Password from Methode to Methode...
      => on MVC I have a SSO and the Server knows me



      System.Web.HttpContext.Current.User.Identity


      It is possible to Use this Information?



      new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???


      Or MUST I give the password. And how to best pass from view to this method.



      thanks










      share|improve this question














      I have a small web server running ASP.NET MVC on it. The Server is running with User "abc" but the User "abc" do not have rights for "changes" in ActiveDirectory.



      So I have to pass the user login in the PrincipalContext with.



      using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
      {

      GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

      theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

      theGroup.Save();

      }


      the Code does work. But I do not like to transfair a Password from Methode to Methode...
      => on MVC I have a SSO and the Server knows me



      System.Web.HttpContext.Current.User.Identity


      It is possible to Use this Information?



      new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???


      Or MUST I give the password. And how to best pass from view to this method.



      thanks







      asp.net-mvc model-view-controller active-directory principalcontext usersession






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 6:32









      beari7beari7

      409




      409
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:



          using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
          // Anything done here will use the user's credentials
          using (var context = new PrincipalContext(ContextType.Domain)) {
          ...
          }
          }





          share|improve this answer


























          • this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

            – beari7
            Nov 23 '18 at 10:06











          • You are creating the PrincipalContext inside the using?

            – Gabriel Luci
            Nov 23 '18 at 11:45











          • And you removed the user, password from the PrincipalContext constructor?

            – Gabriel Luci
            Nov 23 '18 at 12:46











          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%2f53425083%2fc-sharp-activedirectory-get-principalcontext-with-rights%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









          1














          This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:



          using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
          // Anything done here will use the user's credentials
          using (var context = new PrincipalContext(ContextType.Domain)) {
          ...
          }
          }





          share|improve this answer


























          • this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

            – beari7
            Nov 23 '18 at 10:06











          • You are creating the PrincipalContext inside the using?

            – Gabriel Luci
            Nov 23 '18 at 11:45











          • And you removed the user, password from the PrincipalContext constructor?

            – Gabriel Luci
            Nov 23 '18 at 12:46
















          1














          This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:



          using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
          // Anything done here will use the user's credentials
          using (var context = new PrincipalContext(ContextType.Domain)) {
          ...
          }
          }





          share|improve this answer


























          • this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

            – beari7
            Nov 23 '18 at 10:06











          • You are creating the PrincipalContext inside the using?

            – Gabriel Luci
            Nov 23 '18 at 11:45











          • And you removed the user, password from the PrincipalContext constructor?

            – Gabriel Luci
            Nov 23 '18 at 12:46














          1












          1








          1







          This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:



          using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
          // Anything done here will use the user's credentials
          using (var context = new PrincipalContext(ContextType.Domain)) {
          ...
          }
          }





          share|improve this answer















          This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:



          using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
          // Anything done here will use the user's credentials
          using (var context = new PrincipalContext(ContextType.Domain)) {
          ...
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 12:48

























          answered Nov 22 '18 at 14:51









          Gabriel LuciGabriel Luci

          10.6k11424




          10.6k11424













          • this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

            – beari7
            Nov 23 '18 at 10:06











          • You are creating the PrincipalContext inside the using?

            – Gabriel Luci
            Nov 23 '18 at 11:45











          • And you removed the user, password from the PrincipalContext constructor?

            – Gabriel Luci
            Nov 23 '18 at 12:46



















          • this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

            – beari7
            Nov 23 '18 at 10:06











          • You are creating the PrincipalContext inside the using?

            – Gabriel Luci
            Nov 23 '18 at 11:45











          • And you removed the user, password from the PrincipalContext constructor?

            – Gabriel Luci
            Nov 23 '18 at 12:46

















          this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

          – beari7
          Nov 23 '18 at 10:06





          this solution does not Work with PrincipalContext!? I think I'm doing something wrong. if I hand over the password directly, its possible to add users... the callback with WindowsIdentity is: "Access is denied"

          – beari7
          Nov 23 '18 at 10:06













          You are creating the PrincipalContext inside the using?

          – Gabriel Luci
          Nov 23 '18 at 11:45





          You are creating the PrincipalContext inside the using?

          – Gabriel Luci
          Nov 23 '18 at 11:45













          And you removed the user, password from the PrincipalContext constructor?

          – Gabriel Luci
          Nov 23 '18 at 12:46





          And you removed the user, password from the PrincipalContext constructor?

          – Gabriel Luci
          Nov 23 '18 at 12:46


















          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%2f53425083%2fc-sharp-activedirectory-get-principalcontext-with-rights%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga