Skip to content Skip to sidebar Skip to footer

Ado.net - Connect To Sql Server With Windows Login With Login And Password Supplied By The Application

This is SQL Server 2008 R2, .NET 4.0. In my SQL Server there is a user created with 'Windows Authentication'. The user exists in a Active Directory domain. I want to make a .NET a

Solution 1:

I've done it using this class:

https://platinumdogs.me/2008/10/30/net-c-impersonation-with-network-credentials/

You must impersonate using LOGON32_LOGON_NEW_CREDENTIALS = 9 if the computer does not belong to the domain.

Once impersonated, then connect to SQL using "Integrated Security=true" on the SQL Connection String.

SqlConnection conn;
using (new Impersonator("myUserName", "myDomain", "myPassword", LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_DEFAULT))
{
    conn = new SqlConnection("Data Source=databaseIp;Initial Catalog=databaseName;Integrated Security=true;");
    conn.Open();
}
//(...) use the connection at your will.//Even after the impersonation context ended, the connection remains usable.

Post a Comment for "Ado.net - Connect To Sql Server With Windows Login With Login And Password Supplied By The Application"