0

I have a console application which connects to two different SQL databases. I'm using the "impersonate" tag within the config file to force the application to login as "APP_USER". The APP_USER account utilizes windows authentication & has been granted permissions in both databases.

The first DB connection works fine, but the second one fails as it is trying to log in as my account which does not have access.

System.Data.SqlClient.SqlException: Login failed for user 'DOMAIN\CURRENT_USER'.

What do I need to change in my config to make the application login to both databases as another user?

<configuration>
  <connectionStrings>
    <add name="Connection1" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DB1;initial catalog=DBcat1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="Connection2" connectionString="metadata=res://*/Models.Model2.csdl|res://*/Models.Model2.ssdl|res://*/Models.Model2.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DB2;initial catalog=DBcat2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <identity impersonate="true" userName="DOMAIN\APP_USER" password="password"/>
  </system.web>
</configuration>
Rethic
  • 1,054
  • 4
  • 21
  • 36

1 Answers1

0

Apparently the config-setting route can only be used in asp.net projects. In order to accomplish this within a console application, you have to write code that utilizes LogonUser within the advapi32.dll library.

Similar question: StackOverflow: Windows Impersonation from C#

I based my final code off of the following project: Code Project: A small C# Class for impersonating a User

Everything seems to be working fine now.

Community
  • 1
  • 1
Rethic
  • 1,054
  • 4
  • 21
  • 36