title: Using `new PrincipalContext(ContextType.Domain, "domain.name.com")` without having to provide the username and password
body: <p>I've built an application that will be deployed with ClickOnce that, upon startup, will need to check the currently logged in user's Identity/Name, and compare it to the Active Directory Groups on our domain in order to set up appropriate permissions in the application.</p>
  
  <p>I can do this by "hard coding" a username and password into the application, and passing these credentials into the <code>PrincipalContext</code> constructor, i.e.,</p>
  
  <pre><code>var pc = new PrincipalContext(ContextType.Domain, "domain.name.com", username, password);
  </code></pre>
  
  <p>The problem is that I don't want to have to store the username and password in the application (and I don't want to have to prompt the user for their username and password, either).</p>
  
  <p>I've seen many examples online using the constructor without specifying credentials, but this doesn't work on our domain.</p>
  
  <p><strong>Is there a setting/permission in the Domain or "Domain Users" Group properties that I can configure to get this to work?</strong></p>
  
  <p>My current code is as follows (note that :</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Gets a list of all Active Directory Groups the specified username is a member of.
  /// &lt;/summary&gt;
  /// &lt;param name="userName"&gt;The username of the user in DOMAIN\Username format&lt;/param&gt;
  /// &lt;param name="domain"&gt;The name of the domain server, e.g., server.domain.com (not DOMAIN or DOMAIN.COM or SERVER)&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static List&lt;string&gt; GetUserGroups(string userName, string domain)
  {
      if (userName.IsNullOrWhiteSpace()) return null;
  
      var pc = new PrincipalContext(ContextType.Domain, domain);
      var userPc = UserPrincipal.FindByIdentity(pc, userName);
  
      if (userPc == null) return null;
  
      var src = userPc.GetGroups(pc);
      var result = new List&lt;string&gt;();
      src.ToList().ForEach(sr =&gt; result.Add(sr.SamAccountName));
      return result;
  }
  </code></pre>
  
  <p>And I'm essentially calling is like this:</p>
  
  <pre><code>var userGroups = GetUserGroups(WindowsIdentity.GetCurrent.Name, "server.domain.com");
  </code></pre>