title: Getting "Access is denied." when trying to update UserPrincipal object
body: <p>I have a SharePoint solution with a custom application where a user should be able to change some properties in his own Active Directory object.</p>
  
  <p>I am doing the following:</p>
  
  <pre><code>PrincipalContext ctx = ActiveDirectory.GetPrincipalContext("lab");
  UserPrincipal user = ActiveDirectory.GetUserPrincipal(ctx, "Administrator");
  
  user.DisplayName = user.DisplayName + DateTime.Now.ToString("ddMMyyyyHHmmss");
  user.Save();
  </code></pre>
  
  <p>I am logged in to SharePoint as the domain administrator and i am trying to change my own DisplayName.</p>
  
  <p>What is wrong with my code?</p>
  
  <p><strong>Update 20.04.2016</strong></p>
  
  <p>I have built a small Console Application with the following code:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          try
          {
              PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "lab");
              UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "administrator");
  
              DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
  
              user.DisplayName = user.DisplayName + DateTime.Now.ToString("ddMMyyyyHHmmss");
              user.Save();
  
              Console.WriteLine("OK");
              Console.ReadKey();
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
  </code></pre>
  
  <p>This works fine! That means normally, that the Authentication goes wrong or the user which is logged into SharePoint is not getting used to connect to AD and do the changes. If this could be the case, how could i find out with which user i am doing the operation?</p>
  
  <p><strong>Update 20.04.2016 - 2</strong></p>
  
  <p>I have now tried to put the username and password in the PrincipalContext contructor as below:</p>
  
  <pre><code>PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "lab", "administrator", "pass");
  </code></pre>
  
  <p>This works, too! That means now definitely, that the user which is logged in to SharePoint is not used to create the PrincipalContext. But why? <strong>Normally code is always executed in the context of the current user!?</strong></p>
  
  <p>How can i find out which user is used to create the PrincipalContext and how can i change it that the logged in user is getting used?</p>