0

I got this Connection String in C#:

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

It always tries to Login using the User that is logged on on my development machine which I don't want. How can I fix this?

Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
Ghosty
  • 71
  • 1
  • 2
  • 7
  • change `UID=domain\user; Password=password;` this line? Also show how you create context – teo van kot Dec 21 '15 at 11:23
  • What do you want to do (you tell us what you don't want but we have to make assumptions about what you do want)? Specifically are you trying to log in as a different windows user or as an SQL Server user? – Murph Dec 21 '15 at 12:24

2 Answers2

7

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

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • If you are connecting to a remote server (i.e. not your local machine), you may also be able to do this using `NET USE` http://stackoverflow.com/questions/5237654/sql-server-management-studio-2008-runas-user-on-different-domain-over-vpn/5362712#5362712 This will only work for connecting to the server as a single account though - you can't use multiple accounts simultaneously with this. – Ben Nov 08 '16 at 10:42
0

You can find the SQL Server Management Studio in the Start Menu, hold down shift, right click on it and choose "Run as different user". Essentially the same as the runas.exe option, maybe quicker to find for a one-off use, but no option to save credentials either.