从 Delphi 应用程序使用 Windows 身份验证连接到 Sql Server 时以编程方式更改用户名

发布于 2024-08-13 04:51:43 字数 585 浏览 3 评论 0原文

我有一个使用 Windows 身份验证的 Sql Server。

我想从 Delphi 应用程序连接到服务器。

默认情况下,SQL Server 将采用启动连接进程的用户的凭据。

这意味着要更改登录名,我当前有两个选项:

  1. 注销并根据需要登录 用户,然后运行我的应用程序

  2. 从命令启动程序 使用 RUNAS 命令行。

我想让用户从应用程序内提供凭据,并以该用户身份登录。通过操作 ConnectionString 或以编程方式更改当前进程的用户是否可能?

我发现的最接近的是此条目,它告诉我如何在特定凭据下启动另一个进程。我可以使用该技术创建一个“启动器”程序,在从用户那里收集凭据后启动连接过程,但我真的想要更干净的东西。

我在这个项目中使用 Delphi 2010。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分開簡單 2024-08-20 04:51:43

我认为 LogonUser 然后 ImpersonateLoggedOnUser< /code>将为您解决问题。这应该更改当前进程正在运行的用户帐户。正如 gbn 提到的,在更改登录凭据之前,您可能必须断开任何活动连接。

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.

梦幻的味道 2024-08-20 04:51:43

使用 Scott W 建议的方法,这段代码对我有用。其中一些可能需要根据您的特定网络环境进行调整。

procedure ChangeLoggedInUser(username, password, domain: string);
var
  creds: Cardinal;
begin
  try
    if LogonUser(PChar(username)
        ,PChar(domain)
        ,PChar(password)
        ,LOGON32_LOGON_NETWORK
        ,LOGON32_PROVIDER_DEFAULT
        ,creds
      )
    then begin
      ImpersonateLoggedOnUser(creds);
    end
    else begin
      RaiseLastOSError;
    end;
  finally
    //wipe the memory for security
    FillChar(username,SizeOf(username),#0);
    FillChar(password,SizeOf(username),#0);
    FillChar(domain,SizeOf(username),#0);
  end;  //try-finally
end;

这段代码可以这样调用:

...
//at this point i am logged in as whoever is logged into the local machine
DoSomethingMundane;

//change credentials of the current thread
ChangeLoggedInUser('importantuser','secretpassword','mydomain');

//now my process will be logged in as "importantuser"
DoSomethingThatRequiresCreds;

//go back to normal
RevertToSelf;

//now my process is back to normal
DoSomethingMundane;

Using the method suggested by Scott W, this code worked for me. Some of this may need to be tweaked based on your specific network environment.

procedure ChangeLoggedInUser(username, password, domain: string);
var
  creds: Cardinal;
begin
  try
    if LogonUser(PChar(username)
        ,PChar(domain)
        ,PChar(password)
        ,LOGON32_LOGON_NETWORK
        ,LOGON32_PROVIDER_DEFAULT
        ,creds
      )
    then begin
      ImpersonateLoggedOnUser(creds);
    end
    else begin
      RaiseLastOSError;
    end;
  finally
    //wipe the memory for security
    FillChar(username,SizeOf(username),#0);
    FillChar(password,SizeOf(username),#0);
    FillChar(domain,SizeOf(username),#0);
  end;  //try-finally
end;

This code can be called like so:

...
//at this point i am logged in as whoever is logged into the local machine
DoSomethingMundane;

//change credentials of the current thread
ChangeLoggedInUser('importantuser','secretpassword','mydomain');

//now my process will be logged in as "importantuser"
DoSomethingThatRequiresCreds;

//go back to normal
RevertToSelf;

//now my process is back to normal
DoSomethingMundane;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文