[FIXED] Change username programmatically when connecting to a Sql Server using Windows authentication from a Delphi application

Issue

I have a Sql Server that uses Windows Authentication.

I want to connect to the server from a Delphi application.

By default, SQL Server will assume the credentials of the user that launches the connecting process.

This means that to change the login, I currently have two options:

  1. Log off and Log in as the desired user, then run my application

  2. Launch the program from the command line using the RUNAS command.

I'd like to let the user provide credentials from within the application, and log in as that user. Is this possible, either by manipulating the ConnectionString or by programatically changing the user of the current process?

The closest I've found is this entry, which tells me how to launch another process under specific credentials. I could use that technique to create a "launcher" program that launches the connecting process after gathering credentials from the user, but I'd really like something cleaner.

I'm using Delphi 2010 for this project.

Thanks


Solution

I think a combination of LogonUser and then ImpersonateLoggedOnUser will do the trick for you. That should change the user account for which the current process is running. As gbn mentioned, you will likely have to disconnect any active connection before changing the logon credentials.



Answered By - Scott W
Answer Checked By - Katrina (sql.easybugfix Volunteer)

Related Posts

Post a Comment