Reputation: 5576
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.
But when we run the same test case on our build server, the test case fails saying Microsoft.AnalysisServices.AdomdClient.AdomdErrorResponseException: Either the user, 'domain\ttwp5434203$', does not have access to the 'Employee' database, or the database does not exist.
To overcome this problem, I am thinking of impersonating the user under which the test case runs.
I added following code in App.Config file under test project.
<system.web>
<identity impersonate="true"
userName="UID"
password= "PWD" />
</system.web>
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.
Another option I am looking to use is: Impersonate(IntPtr). But I am not sure how can I use this code across multiple test cases.
Upvotes: 0
Views: 2195
Reputation: 5576
I am now running my test cases using SimpleImpersonation library. This nugget package allows you to impersonate some user and its really easy to use.
[TestMethod]
public void SearchUser()
{
var credentials = new UserCredentials("domain", "UID", "PWD");
var result = Impersonation.RunAsUser(credentials, LogonType.NewCredentials, () =>
{
//CODE INSDIE THIS BLOCK WILL RUN UNDER THE ID OF ANOTHER USER
dynamic actualResult = controller.SearchUser();
//Assert
Assert.IsNotNull(actualResult);
return actualResult;
});
}
Upvotes: 1