title: Kerberos double-hop in ASP.NET 4.0 & SQL2008R2
body: <p>I have an ASP.NET 4.0 application within which I need to forward the authentication to the database.
  For the purposes of this request for assistance, lets call the web server "app1" and the database server "sql1".</p>
  
  <p>The SQL2008R2 database service is running as a named instance "SQL2008R2" under a custom domain account "SqlServer". The server is running Windows Server 2008 R2 Enterprise Edition.
  I have created an SPN for this...</p>
  
  <pre><code>setspn -a MSSQLSvc/sql1.mydomain.local:SQL2008R2 SqlServer
  </code></pre>
  
  <p>The ASP.NET application is running under an application pool using a custom domain account "WebApplicationUser", in Integrated Pipeline mode. It is currently running on my laptop running Windows 7 Enterprise, but will eventually be hosted on Windows Server 2008 R2 Standard Edition.
  I have created 2 SPN's for the application (on the Windows 7 machine that I am currently running from)...</p>
  
  <pre><code>setspn -a http/app1 WebApplicationUser
  setspn -a http/app1.mydomain.local WebApplicationUser
  </code></pre>
  
  <p>Within Active Directory users and Computers, I have selected the "WebApplicationUser" account and I have enabled constrained delegation to "MSSQLSvc/sql1.mydomain.local:SQL2008R2" using any protocol (I have also tried using Kerbero only).</p>
  
  <p>The Application is setup in IIS 7.5 and the authentication is set to disable Anonymouse, Basic, Digest and Forms whilst enabling "ASP.NET Impersonation" and "Windows". The Windows authentication has "Extended protection" turned off and "Kernel-mode authentication" enabled. The providers are "Negotiate" and "NTLM" in that order.</p>
  
  <p>The ASP.NET application uses EF, and the connection string is configured to use integrated security...</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="MyContext"
               connectionString="metadata=res://*/Data.MyModel.csdl|res://*/Data.MyModel.ssdl|res://*/Data.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=sql1.mydomain.local\sql2008r2;Initial Catalog=MyDatabase;Persist Security Info=false;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;"
               providerName="System.Data.EntityClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>My web config specifies both Windows authentication and impersonation, since I a using async pages, I have also enabled inpersonation policy flowing...</p>
  
  <pre><code>&lt;runtime&gt;
      &lt;alwaysFlowImpersonationPolicy enabled="true" /&gt;
  &lt;/runtime&gt;
  
  &lt;system.web&gt;
      &lt;authentication mode="Windows" /&gt;
      &lt;identity impersonate="true" /&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p>If I log on locally (on "web1") and browse to the application (using IE), this all works - but this does not involve the double hop that I am trying to resolve.</p>
  
  <p>If I log on to another machine and then browse to the application using IE, or I browse from the local machine using FireFox, this does not work - note: FireFox does prompt me for the login details. The connection to the database fails with "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'"</p>
  
  <p>Unlike a lot of the articles (and here might be part of the problem), I am not using any custom code to impersonate the user. It is my understanding that the impersonation will be applied across the board to the application by the web.config settings above. All I do is to open the connection and then close it again when I am finished with it.</p>
  
  <p>I have obviously missed a step (or two), but having looked at all of the documentation that I can find (and there has been a lot), I still cannot find what that step is. It does not help that 99% of the documentation that I can find is actually related to IIS6 and Windows 2003 but the principles should remain the same.</p>
  
  <p>Has anybody suceeded in getting such a configuration to work on Windows 7 and/or Windows Server 2008?</p>