title: Acces denied error when trying to save a distribution group
body: <p>When running the code shown below, I am getting an error that states:</p>
  
  <p>An unhandled exception of type 'System.UnauthorizedAccessException' occurred in<br>
      System.DirectoryServices.dll. Additional information: Access is denied.</p>
  
  <p>It happens on this line : <code>Group.Save();</code> </p>
  
  <p>I have used the username and password in other scripts relating to Active Directory so it does have sufficient access to anything i need it to do. Although, I haven't tried it in relation to the <code>System.DirectoryServices.AccountManagement</code> library before.</p>
  
  <p>I am using Visual Studio 2013 running in admin mode, so there shouldn't be a problem with the command prompt not running in it.</p>
  
  <p>If anyone can give me a heads up as to why it wouldn't be using the credentials when saving, it would be greatly appreciated.</p>
  
  <pre><code>try
              {
                  using (PrincipalContext PC = new PrincipalContext(ContextType.Domain, "DOMAIN", "USERNAME", "PASSWORD"))
                  {             
                      GroupPrincipal Group = GroupPrincipal.FindByIdentity(PC, "GROUPNAME");
                      DBAccessManager DBAccess = new DBAccessManager();
                      SqlConnection ConnString = new SqlConnection();
  
                      ConnString.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLCONNECTION"].ToString();
                      List&lt;Employee&gt; SQLEmployees = DBAccess.ReadUserInformation(ConnString.ConnectionString.ToString());
  
                      foreach (Employee Emp in SQLEmployees)
                      {
                          UserPrincipal PrinicipalUser = UserPrincipal.FindByIdentity(PC, Emp.USERNAME);
                          UserPrincipal ADUser = UserPrincipal.FindByIdentity(PC, Emp.USERNAME);
                          if (PrinicipalUser.IsMemberOf(Group) &amp;&amp; String.IsNullOrEmpty(Emp.SAPLOGIN))
                          {
                              Group.Members.Remove(ADUser);
                          }
                          else if (!PrinicipalUser.IsMemberOf(Group) &amp;&amp; !String.IsNullOrEmpty(Emp.SAPLOGIN))
                          {
                              Group.Members.Add(ADUser);
                          }
                      }
                      Group.Save();                  
                  }
              }
              catch (DirectoryServicesCOMException E)
              {
                  Console.WriteLine("Exception: " + E.Message);
              } 
  </code></pre>