title: .NET SqlClient login failure from background thread in ASP.NET WebForms application
body: <ul>
  <li>ASP.NET 4.51, WebForms, VS2013</li>
  </ul>
  
  <p>I am using <a href="http://www.quartz-scheduler.net/">Quartz.NET</a> to do some background processing where ultimately I make a connection to my SQL Server. This all works locally on my development machine against IIS Express, but when I deploy it to my staging server running IIS I run into problems.</p>
  
  <p>The code to connect to the database could not be simpler:</p>
  
  <pre><code>myConnection = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=MyDB;User ID=sa;Password=myPass");
  </code></pre>
  
  <p>however this throws an exception of:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: Login failed for user 'IIS
    APPPOOL\somehost.somedomain.com'.</p>
  </blockquote>
  
  <p>I am 100% confident the connection string is correct as when used in a normal page it works just fine. So what is throwing me is the reference to <strong>IIS  APPPOOL</strong>.</p>
  
  <p>Is the SqlConnection somehow not using the connection string it was passed? Doing some form of weird user impersonation when the connection is being made?</p>
  
  <p>Put another way. How do I make the SqlConnection() work from within the thread when I know the connection string is correct?</p>