Sql server 如何在ADODB连接中模拟另一个Windows登录?

Sql server 如何在ADODB连接中模拟另一个Windows登录?,sql-server,iis,asp-classic,Sql Server,Iis,Asp Classic,如何通过ADODB.Connection对象使用指定的Windows凭据集连接到MS SQL Server 例如: Dim oConn Set oConn = CreateObject("ADODB.Connection") Dim oRs Set oRs = CreateObject("ADODB.Recordset") Dim strConnection Dim strSQL strSQL = "SELECT * from Computer WHERE DeviceName = N'my

如何通过ADODB.Connection对象使用指定的Windows凭据集连接到MS SQL Server

例如:

Dim oConn  
Set oConn = CreateObject("ADODB.Connection")
Dim oRs
Set oRs = CreateObject("ADODB.Recordset")
Dim strConnection
Dim strSQL
strSQL = "SELECT * from Computer WHERE DeviceName = N'myDevice'"

oConn.Provider = "SQLOLEDB.1"
oConn.Properties("User ID") = "myDomain\myUser" 
oConn.Properties("Password") = "myPassword" 
oConn.Properties("Initial Catalog") = "myDB"
oConn.Properties("Data Source") = "mySQLServer"
oConn.Open
如果我这样做,那么它将失败,错误如下:

Microsoft OLE DB Provider for SQL Server error '80040e4d' 

Login failed for user 'myDomain\myUser'.
Microsoft OLE DB Provider for SQL Server error '80040e4d' 

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
检查SQL Server日志会显示此错误:

Login failed for user 'myDomain\myUser'. Reason: Could not find a login matching the name provided. [CLIENT: <IP>]
哪些不是我提供的凭据

我怎样才能做到这一点


提前谢谢。

有几件事值得注意。如果您尝试使用windows域帐户登录,则可能尝试使用受信任的连接。您需要将IIS配置为不允许匿名访问,并启用集成的Windows身份验证(这可能因您安装的IIS版本和操作系统而异)

查看以下连接字符串,看看它们是否有用(根据您使用的数据库的不同,这些字符串可能会有所不同——只需通过谷歌搜索相应的数据库):

使用已登录用户凭据的步骤

strConn = "Driver={SQLServer};Server=your_server_name;Database=your_database_name;Trusted_Connection=yes;"
Set cnt = Server.CreateObject("ADODB.Connection")
cnt.ConnectionString= strConn
使用SQL Server用户名和密码(而不是域名和密码)


祝你好运。

IIS配置为不允许匿名访问,并且启用了集成Windows身份验证,打开页面时会提示我输入凭据。使用连接字符串仍然会给我
“用户“NT AUTHORITY\ANONYMOUS LOGON”登录失败。
error您是否能够创建SQL Server用户,然后使用这些凭据?这就是我通常连接到DBs的方式。当我需要从代码中捕获用户信息时,我使用集成身份验证。祝你好运。很抱歉回复晚了。我不知道为什么我不能让它在Win2k3/IIS6上运行,但您建议的是在Win2k8R2/IIS7.5上运行良好。谢谢
strConn = "Driver={SQL Server};Server=mySQLServer;Database=myDB;uid=someUser;pwd=somePass"
Set cnt = Server.CreateObject("ADODB.Connection")
cnt.ConnectionString= strConn