title: UserPrincipal.Current returns apppool on IIS
body: <p>I need to find who is the current user and check their groups in an active directory setup (windows server 2008) to see if they have permission to access certain pages (admin) on the mvc3 site I am constructing.  However, whenever I create a PrincipalContext and query the current user, it returns the apppool the site is running under.</p>
  
  <p>Ive tried:</p>
  
  <pre><code>PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
  UserPrincipal currentuser = UserPrincipal.Current;
  string username = currentuser.DisplayName;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain", "CN=dbcn LDAP,OU=the - account,DC=thedc,DC=local", "domain\\user", "password");
  UserPrincipal currentuser = UserPrincipal.Current;
  string username = currentuser.DisplayName;
  </code></pre>
  
  <p>Web.config looks like:</p>
  
  <pre><code>&lt;configuration&gt;
  &lt;appSettings&gt;
  &lt;add key="webpages:Version" value="1.0.0.0" /&gt;
  &lt;add key="ClientValidationEnabled" value="true" /&gt;
  &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  &lt;add key="autoFormsAuthentication" value="false" /&gt;
  &lt;add key="enableSimpleMembership" value="false"/&gt;
  &lt;/appSettings&gt;
  &lt;authentication mode="Windows" /&gt;
  &lt;membership defaultProvider="AspNetActiveDirectoryMembershipProvider"&gt;
  &lt;providers&gt;
  &lt;clear /&gt;
  &lt;add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="service" /&gt;
  &lt;/providers&gt;
  &lt;/membership&gt;
  &lt;roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"&gt;
  &lt;providers&gt;
  &lt;clear /&gt;
  &lt;add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /&gt;
  &lt;/providers&gt;
  &lt;/roleManager&gt;
  &lt;identity impersonate="false" /&gt;
  &lt;connectionStrings&gt;
  &lt;add name="foocontext" connectionString="data source=foo;Initial Catalog=foo;Integrated Security=SSPI;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" /&gt;
  &lt;add name="ADService" connectionString="LDAP://foo.local/OU=the - service,DC=foo,DC=local" /&gt;
  &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Ive tried instantiating the context with two different accounts (and with no account specified), one of them the ldap account the IT admin uses for queries.  What am I missing here?  Why does it always return the apppool as the current user?  How can I get the current logged in user.</p>
  
  <p>Thanks!</p>