使用另一个域的用户凭据连接到 SQL Server [英] Connect to SQL Server with user credentials of another domain

查看:36
本文介绍了使用另一个域的用户凭据连接到 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用位于另一个域中的用户登录名/密码连接到 SQL Server 数据库?

How can I connect to a SQL Server database using user login/password that is in another domain?

如果我使用我的帐户连接到数据库,它工作正常:

If I use my account to connect to DB, it works fine:

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server_name;DATABASE=testdb;UID=MY_Domain\\username;PWD=pass; Trusted connection=YES')

但我需要使用其他用户的凭据,例如

But I need to use another user's credentials like

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server_name;DATABASE=testdb;UID=Another_Domain\\username;PWD=pass; Trusted connection=YES')

当我尝试后者时,我收到错误MY_Domain\username 登录失败"而不是对于用户Another_Domain\username".

When I try the latter I get an error of "failed login for MY_Domain\username" rather than for the user "Another_Domain\username".

在这两种情况下,通过运行 SQL Server Management Studio,我可以使用 Windows 身份验证连接到数据库.

In both cases by running SQL Server Management Studio I can use Windows Authentication to connect to the db.

推荐答案

您不能传递 UIDPassword 并设置 Trusted_connection=True>(您的第二个连接字符串)以(模拟的)Windows 用户身份连接.您可以作为 SQL Server 用户(用户名和密码)或 Windows 身份验证用户(受信任的连接)进行连接.

You can not pass a UID and Password and set Trusted_connection=True (your second connection string) to connect as a (impersonated) windows user. You can either connect as a SQL Server user (username and password) or as a windows authenticated user (trusted connection).

您的代码应该模拟 Windows 用户(就像 SSMS 所做的那样),然后设置 Trusted_connection=True only.

Your code should impersonate the windows user (as SSMS does) and then set Trusted_connection=True only.

此 MSDN 页面 WindowsIdentity.Impersonate 有一个例子.

This MSDN page WindowsIdentity.Impersonate has an example.

由于这在 SSMS 中起作用,因此表明域之间存在必要的信任.

Since this works from SSMS it suggests there is the necessary trust between the domains.

这篇关于使用另一个域的用户凭据连接到 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆