title: Accessing SQL Server using Integrated Security from an ASMX run in authenticated user context
body: <p>I have a Webservice running in Windows Authentication mode. </p>
  
  <p>I have an application to text the asmx connections:</p>
  
  <pre><code>BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
  BasicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
  basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
  
  EndpointAddress endpoint = new EndpointAddress("http://xyzts/Service.asmx");
  WS_SPIL.ServiceSoapClient client = new WS_SPIL.ServiceSoapClient(basicHttpBinding, endpoint);
  
  client.ClientCredentials.Windows.AllowedImpersonationLevel = Sytem.Security.Principal.TokenImpersonationLevel.Impersonation;
  client.ChannelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
  </code></pre>
  
  <p>I execute the folloing code (on the ASMX): User.Identity.Name which returns my windows AD login.</p>
  
  <p>So far so good. </p>
  
  <p>Now i want to connect to the database with the login in user. 
  But i get the error message that the current user (the IIS user) doesn't have the privilige to connect to the sql server.</p>
  
  <pre><code>try
  {
      SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
      csb.DataSource = "sqlservername";
      csb.InitialCatalog = "DBName";
      csb.IntegratedSecurity = true;
  
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = csb.ToString();
      conn.Open();
      conn.Close();
  
      return "ok";
  }
  catch (Exception ex)
  {
      return ex.ToString();
  }
  </code></pre>