connect with TMSConnection using Windows Authentication using another user

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

connect with TMSConnection using Windows Authentication using another user

Post by brace » Mon 08 May 2017 12:20

Recently I asked you this question:
viewtopic.php?f=6&t=35351

What i am (Still) trying to achieve is the following:
Using TMSConnection to login to a database when Windows authentication is chosen but with another user. Somehow this is like when with LDAP you try to connect with another user instead than yours

Please note whati am looking for could also be a workaround/a trick.

Let me explain you the context:
- in my app each user is mapped to a Sql Server user (either with SQL or Win authentication)
- each user can just select from a table, this table contains the encrypted sa connection string
- the user logs in and just retrieves the encrypted string, then the app connects with sa
- somtimes a user is logged in as a different domain user and he would like to connect to my application without loggin out and in again with the "correct" windows user, but since login is needed just to connect and retrieve the sa connection string and the user has only his domain password to login to the system i would like to tell to Windows "please let me connect to sql serve using win authentication for another user, this is just for a single query i need to run once"

If What i am looking for is not possible with "pure SDAC" i would like to achieve it also using some WinAPI or any trick you can suggest me.
Thanks.

UPDATE:
As i was writing this a realizd a workaround must be there and i foudn the API http://www.michael-puff.de/Programmieru ... gonW.shtml

from a first test it seems possible to create a simple exe that just has a connection and a query inside it so i can retrieve the connection string from database, i will run this exe using CreateProcessWithLogonW. Please comment or suggest a better trick.

Thanks.

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: connect with TMSConnection using Windows Authentication using another user

Post by azyk » Thu 11 May 2017 13:15

To solve the specified issue you can use the T-SQL command EXECUTE AS LOGIN in order to switch between user contexts:

Code: Select all

 EXECUTE AS LOGIN = 'DOMAIN\User1';
More details about EXECUTE AS LOGIN at MSDN:
https://docs.microsoft.com/en-us/sql/t- ... ansact-sql

brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

Re: connect with TMSConnection using Windows Authentication using another user

Post by brace » Thu 11 May 2017 15:10

Thank you.

Post Reply