user2412130
user2412130

Reputation: 55

Connecting to SQL server from c# with other user Windows Credentials

I am using Linq to sql from c# to connect to sql server in a WCF service. I am using windows authentication to connect to database. I want to use other user windows authentication to connect to sql server from Linq to sql. Is their a way to do that.

Upvotes: 1

Views: 2829

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131237

From the comments I assume you want to connect to a production database during development for a short period, until your development account gets access to the database.

First of all, don't do this. It's a bad idea for several reasons. Working against a production database is bad.So is writing code that will be removed once you get proper access.

If you only want to test your service, just use the Windows account credentials you already have as the service account.

As a last resort, you can impersonate a specific account using WindowsIdentity.Impersonate. The function's sample shows how to authenticate the user using P/Invoke and the LogonUser Win32 API. You'll have to take care to call Undo once you are finished impersonating otherwise your code will keep running with the old identity.

If Kerberos is properly implemented in the domain and your account has permission to impersonate the other account, you can use the WindowsIdentity constructor that only needs a user principal name. That's far safer than storing a username and password in a config file, even it you encrypt them.

A better idea is to clone the data you need (all or just a sample) to a local database and use this during development. This way you will be free to experiment without affecting the production environment (or get blamed for anything untoward).

Upvotes: 1

Related Questions