title: Impersonation Issue on IIS7 - Entity Framework Trusted_Connection ASP.NET
body: <p>I'm developing an ASP.NET Application that connects to a Database (on another machine). When I try to connect from my machine there are no problem, when I deploy it on the destination environment, EF returns an exception:</p>
  
  <pre><code>The underlying provider failed on Open.System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at 
  </code></pre>
  
  <p>My configuration is:</p>
  
  <pre><code>    &lt;authentication mode="Windows" /&gt;
      &lt;customErrors mode="Off"&gt;&lt;/customErrors&gt;
      &lt;identity impersonate="true" /&gt;
      &lt;security&gt;
          &lt;authentication&gt;
              &lt;windowsAuthentication useKernelMode="false"&gt;
                  &lt;extendedProtection tokenChecking="None" /&gt;
              &lt;/windowsAuthentication&gt;
          &lt;/authentication&gt;
      &lt;/security&gt;
  </code></pre>
  
  <p>And my connection string is:</p>
  
  <pre><code> &lt;add name="pmgbicopEntities" connectionString="metadata=res://*/copDB.csdl|res://*/copDB.ssdl|res://*/copDB.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=[NAME]\devde;Initial Catalog=[NAME];Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>If I remove the database instruction the this.Page.User is my current Account (and this is right!), but when I open a new connection via EF I become "Anonymous"</p>
  
  <p>I tried with impersonation like:</p>
  
  <pre><code>WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
  WindowsImpersonationContext ctx = null;
  try
  {
    ctx = winId.Impersonate();
    //EF SOMETHING
  }
  catch (Exception exx)
  {
  }
  finally
  {
    if (ctx != null)
    ctx.Undo();
  }
  </code></pre>
  
  <p>But I get the same exception "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."</p>
  
  <p>I tried to change the application pool user, tried to impersonate manually but without any success.</p>
  
  <p>The IIS configuration has some kerberos stuff that I tried to Enable and Disable but nothing change.</p>
  
  <p>Do you have any suggestion? Do I have to enable something like "trust" between machines? For my development machine from "fake IIS" I have no problem but my user is enabled on the DB.</p>
  
  <p>Thank you in advance.</p>