Search code examples
c#sql.netsql-serverwindows-authentication

SQL Server Windows Auth using other Credentials than logged on User


I have this connection string in C#:

strg_SQL_Connection = @"Server=Server\Instance; Database=DB;UID=domain\user; Password=password;";

It always tries to login to the SQL environment using the user that is logged in on my development machine, which I don't want. How can I fix this?


Solution

  • You cannot use Windows credentials from a connection string. Your process, or at least your thread, must actually impersonate the desired user before establishing the connection. There are many ways you can impersonate, refer to MSDN for details. See WindowsIdentity.Impersonate() and/or Client Impersonation.

    A trivial way to achieve impersonation is using runas.exe