title: Logon failure error when attempting UserPrincipal.FindByIdentity
body: <p>I've been searching for solutions on this for a while now, but each thread a dead-end unfortunately.</p>
  
  <p>I'm working on a C#/ASP.net web app that will only be used internally by our company. Anonymous access has been switched off both in IIS and my web.config file forcing IIS to use the windows authenticated user (Active Dir user).</p>
  
  <p>My problem is that the following code works perfectly to get the required (or any) AD user: </p>
  
  <pre><code>using System.DirectoryServices.AccountManagement;
  
  ... other code ...
  
  PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain", "someADuser", "someADuserpassword");
  UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "samaccountname");
  </code></pre>
  
  <p>"someADuser" used in the PrincipalContext above is the current logged in user through windows, thus authenticated and a valid AD user. Using the following code (with the exact same user still logged in) gives me a "Logon failure: unknown user name or bad password" error:</p>
  
  <pre><code>PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain");
  UserPrincipal winuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "samaccountname");
  </code></pre>
  
  <p>It seems UserPrincipal.FindByIdentity doesn't use the logged in user's validated credentials for some reason if it's not specified in the PrincipalContext object - something I don't want to do.</p>
  
  <p>Is it possible that the ctx aren't picking up the logged in Windows users for some reason, even if the necessary settings (i hope) is added to web.config :</p>
  
  <pre><code>&lt;authentication mode="Windows"/&gt;
  &lt;authorization&gt;
    &lt;deny users="?"/&gt;
  &lt;/authorization&gt;
  </code></pre>
  
  <p>And Anonymous access is completely disabled in IIS?</p>