title: C# - Asp.net web API test project - how to impersonate a user and run test cases under a specific user
body: <p>I am working on ASP.net Web api project. I have added a test project. Inside one of the test case, I connect to SQL server using windows authentication. The test case works fine locally when I run it on Visual Studio as my account (my NT ID) is entitled to the SQL server. </p>
  
  <p>But when we run the same test case on our build server, the test case fails saying <code>Microsoft.AnalysisServices.AdomdClient.AdomdErrorResponseException: Either the user, 'domain\ttwp5434203$', does not have access to the 'Employee' database, or the database does not exist.</code> </p>
  
  <p>To overcome this problem, I am thinking of impersonating the user under which the test case runs. </p>
  
  <p>I added following code in App.Config file under test project.</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;identity impersonate="true"
                userName="UID"
                password= "PWD" /&gt;
    &lt;/system.web&gt;
  </code></pre>
  
  <p>This change did not worked. I got the same error again. What can I do to ensure test case on any machine runs under a specific account. </p>
  
  <p>Another option I am looking to use is: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.impersonate?view=netframework-4.5#System_Security_Principal_WindowsIdentity_Impersonate_System_IntPtr_" rel="nofollow noreferrer">Impersonate(IntPtr)</a>. But I am not sure how can I use this code across multiple test cases. </p>