从 Delphi 应用程序使用 Windows 身份验证连接到 Sql Server 时以编程方式更改用户名
我有一个使用 Windows 身份验证的 Sql Server。
我想从 Delphi 应用程序连接到服务器。
默认情况下,SQL Server 将采用启动连接进程的用户的凭据。
这意味着要更改登录名,我当前有两个选项:
注销并根据需要登录 用户,然后运行我的应用程序
从命令启动程序 使用 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:
Log off and Log in as the desired
user, then run my applicationLaunch 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
LogonUser
然后ImpersonateLoggedOnUser< /code>
将为您解决问题。这应该更改当前进程正在运行的用户帐户。正如 gbn 提到的,在更改登录凭据之前,您可能必须断开任何活动连接。
I think a combination of
LogonUser
and thenImpersonateLoggedOnUser
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.使用 Scott W 建议的方法,这段代码对我有用。其中一些可能需要根据您的特定网络环境进行调整。
这段代码可以这样调用:
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.
This code can be called like so: