尽管我发现了许多有相同错误的帖子,但我找不到我的错误是否可以修复。
有一个数据库,我在网络上用我的窗口登录拥有读取权限。我只是使用SSMS来访问表,但那并不是世界上最好的接口,所以我写了一个小网站,可以连接到表,过滤我想要的数据,并很好地显示它们。
我使用Integrated Security = SSPI (也尝试过TRUE),直到现在都运行得很好。我使用VS2010来运行这个网站。但是使用VS2010运行一个网站并不是一件理想的事情,所以我把我的网站放在机器上的IIS7上(即本地主机:xxx)。这就是我得到上述错误的时候。
如果我能让数据库中的用户只拥有我想要读取的表的读取权限,那么所有这些问题都可以解决,但在我的情况下,这不是简单的可能。我无法对数据库进行任何更改。
那么,有没有办法在本地IIS上使用连接字符串托管网站,该字符串使用集成安全性并使用window登录凭据连接到远程数据库?
谢谢。
发布于 2013-06-10 23:48:36
如果您知道在集成安全登录中使用的Windows用户的用户ID/密码,可以尝试以下方法。
首先声明对Windows API函数的调用:
Private Enum LogonSessionType As Integer
Interactive = 2
Network
Batch
Service
NetworkCleartext = 8
NewCredentials
End Enum
Private Enum LogonProvider As Integer
WinDefault = 0
WinNT35
WinNT40
WinNT50
End Enum
<DllImport("advapi32.dll", SetLastError:=True)> _
Private Shared Function LogonUser(ByVal userID As String, _
ByVal domain As String, _
ByVal password As String, _
ByVal logonType As LogonSessionType, _
ByVal LogonProv As LogonProvider, _
ByRef token As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function然后声明2个辅助函数
Sub BeginImpersonate(ByVal i_sUserID As String, ByVal i_sPassword As String, ByRef o_impersonatedUser As WindowsImpersonationContext, ByRef o_token As IntPtr)
o_token = IntPtr.Zero
o_impersonatedUser = Nothing
Dim bLoginSuccessful As Boolean = LogonUser(i_sUserID, Nothing, i_sPassword, LogonSessionType.Interactive, LogonProvider.WinDefault, o_token)
If bLoginSuccessful Then
Dim id As New WindowsIdentity(o_token)
o_impersonatedUser = id.Impersonate()
Else
Throw New Exception ("Logon failed: Error " & Marshal.GetLastWin32Error.ToString)
End If
End Sub
Sub EndImpersonate(ByVal i_impersonatedUser As WindowsImpersonationContext, ByVal i_token As IntPtr)
If i_impersonatedUser IsNot Nothing Then i_impersonatedUser.Undo()
If i_token <> IntPtr.Zero Then CloseHandle(i_token)
End Sub做好准备后,您可以像这样进行调用:
Dim impersonatedUser As WindowsImpersonationContext = Nothing
Dim token As IntPtr = IntPtr.Zero
BeginImpersonate(i_sUserID, i_sPassword, impersonatedUser, token)
'Do your DB stufff here, open connection etc.
EndImpersonate(impersonatedUser, token)这段代码有点原始,但它可以工作。您将需要添加适当的错误处理等,以使其成为生产代码。在i_sUserID参数中传入user @domain格式的用户ID,在i_sPassword参数中传入用户密码。
https://stackoverflow.com/questions/17026817
复制相似问题