title: Windows Authentication not working with ASP.NET webpage
body: <p>I'm trying to hit my intranet website and get it to run a simple sql query as the windows user I'm logged in as.</p>
  
  <p>When I debug through Visual Studio, everything works great. When I hit the webserver though, I get an error from sqlconnection saying, "ERROR:Login failed for user 'YOUR_DOMAIN\YOUR_WEBSERVER_NAME'."</p>
  
  <pre><code>Request.ServerVariables[AUTH_USER]: YOUR_DOMAIN\UserBob 
  System.Security.Principal.WindowsIdentity.GetCurrent().Name: NT AUTHORITY\NETWORK SERVICE
  Page.User.Identity.Name: YOUR_DOMAIN\UserBob
  System.Threading.Thread.CurrentPrincipal.Identity.Name: YOUR_DOMAIN\UserBob
  </code></pre>
  
  <p>So how do I get the SQL query to execute under UserBob?</p>
  
  <p>Here's my setup:</p>
  
  <pre><code>  &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0"/&gt;
      &lt;authentication mode="Windows"/&gt;
      &lt;identity impersonate="true"/&gt;
      &lt;customErrors mode="Off"/&gt;
    &lt;/system.web&gt;
  </code></pre>
  
  <p>Webserver is a Win 2008 server with IIS7, Windows Authentication on, Anon Auth off.</p>
  
  <p>Code is simply:</p>
  
  <pre><code>Response.Write("Request.ServerVariables[AUTH_USER]: " + Request.ServerVariables ["AUTH_USER"].ToString());
  Response.Write("&lt;br&gt;System.Security.Principal.WindowsIdentity.GetCurrent().Name: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);
  Response.Write("&lt;br&gt;Page.User.Identity.Name: " + Page.User.Identity.Name);
  Response.Write("&lt;br&gt;System.Threading.Thread.CurrentPrincipal.Identity.Name: " + System.Threading.Thread.CurrentPrincipal.Identity.Name);
  
  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CarbonDB"].ConnectionString);
  
  conn.Open();
  
  SqlCommand sqlcom = new SqlCommand("dbo.runsomething", conn);
  sqlcom.CommandType = CommandType.StoredProcedure;
  SqlDataReader sqlDataReader = sqlcom.ExecuteReader();
  
  conn.Close();
  </code></pre>