title: SQL CLR function fails to impersonate when SQL Server is running as a domain account instead of SYSTEM or NETWORK SERVICE
body: <p>I have the following code working perfectly on a test box, which runs the MSSQLSERVER service as SYSTEM:</p>
  
  <pre><code>      currentIdentity = SqlContext.WindowsIdentity;
        impersonatedIdentity = currentIdentity.Impersonate();
  
         client.PreAuthenticate = true;
         client.Credentials = CredentialCache.DefaultNetworkCredentials;
         client.Url = currentURL;
  
     /* Encrypt */
     try
     {
        encryptedText = client.Encrypt(plainText);
     }
     finally
      {
          if (impersonatedIdentity != null)
          {
              impersonatedIdentity.Undo();
          }
      }
  
          return encryptedText;
  </code></pre>
  
  <p>The basic idea of this function is to encrypt/decrypt text via a web service hosted internally. Whenever this function is called on a server that is running the MSSQLSERVER service as 'SYSTEM' or some other local account, the impersonation works fine. If the server is running the MSSQLSERVER service as a domain account (i.e. DOMAIN\sqlaccount), the credentials of 'DOMAIN\sqlaccount' are always passed to the web service, and impersonation does not seem to be working.</p>
  
  <p>Is this expected behavior? Thanks in advance.</p>