O. San
O. San

Reputation: 1177

sql connection string windows authentication c# winforms (not integrated)

I am making a simple c#.net winform application which will connect to sql server. I wnat it to have 2 ways to connect- Windows Authentication and SQL server Authentication.

From what I found online I came up so far with:

public static void SetConnectionStringParams(string dbAddress, bool isWinAuth, string user, string password)
        {
            if (isWinAuth)
            {
                _connectionString =
                    string.Format(
                        "Data Source={0};Database = {1};Integrated Security=True;Max Pool Size=1000;MultipleActiveResultSets=True;Connection Timeout=60",
                        dbAddress, DefaultDBname);
            }
            else
            {
                _connectionString =
                    string.Format(
                        "Data Source={0};Database = {1};User ID={2};Password={3};Max Pool Size=1000;MultipleActiveResultSets=True;Connection Timeout=60",
                        dbAddress, DefaultDBname, user, password);
            }
        }

which works great, but I couldn't found how in case of Windows Auth I force require user and password instead of using the Integrated Security=True...

Is there an API that do that? if not, would appreciate guidance how to do it.

Thank you,

Upvotes: 0

Views: 2605

Answers (1)

SACn
SACn

Reputation: 1924

Give this a shot

  1. Ask user to enter windows username and password
  2. Impersonate this user manually as by Microsoft and fetch users Principal & Identity
  3. Now fork new thread with this Principal/Identity to talk with database with integrated security
  4. Flush user, identity and principal on job completion or on exception

Upvotes: 1

Related Questions