title: C# DirectoryEntry Properties Value Change
body: <p>I want to use C# to </p>
  
  <ol>
  <li>Create a new user.</li>
  <li>Add it to "Users" group</li>
  <li>Enable Dial-in Remote Network Permission</li>
  <li>Disable remote control</li>
  </ol>
  
  <p>My code:</p>
  
  <pre><code>PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
  UserPrincipal newuser = new UserPrincipal(ctx, "newuser", "password", true);
  newuser.Save();
  
  GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx,"Users");
  
  using (DirectoryEntry groupEntry = group.GetUnderlyingObject() as DirectoryEntry )
  using (DirectoryEntry userEntry = newuser.GetUnderlyingObject() as DirectoryEntry)
  {
      groupEntry.Invoke( "Add", new object[] { userEntry.Path } );
      userEntry.RefreshCache();
  
      userEntry.Properties["msNPAllowDialin"].Value = true;
      userEntry.Properties["msTSRemoteControl"].Value = false;
  
      userEntry.CommitChanges();
  }
  </code></pre>
  
  <p>The account creation and user group process works fine, however, </p>
  
  <pre><code>userEntry.Properties["msNPAllowDialin"].Value = true;
  userEntry.Properties["msTSRemoteControl"].Value = false;
  </code></pre>
  
  <p>Both generates an error </p>
  
  <blockquote>
    <p>The directory property cannot be found in the cache</p>
  </blockquote>
  
  <p>I have spent 2 hours Googling but still can't find any working solutions.</p>