“模拟”用户未传播到 SQL Server 2000

Posted

技术标签:

【中文标题】“模拟”用户未传播到 SQL Server 2000【英文标题】:"Impersonated" User Not Propagated To SQL Server 2000 【发布时间】:2009-08-07 21:08:04 【问题描述】:

我需要在 VB.NET 2008 WinForms 应用程序中“模拟”一个用户,以便该应用程序可以接受 PC 上任何用户的 Active Directory 登录,而不管谁实际登录到 Windows。我希望应用程序的 My.User 成为登录应用程序的人的 AD 帐户。我使用以下代码成功地做到了这一点:

Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, ByVal lpszDomain As String, _
                                                            ByVal lpszPassword As String, ByVal dwLogonType As Integer, _
                                                            ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Boolean

Const LOGON32_LOGON_INTERACTIVE As Long = 2
Const LOGON32_LOGON_NETWORK As Long = 3

Const LOGON32_PROVIDER_DEFAULT As Long = 0
Const LOGON32_PROVIDER_WINNT35 As Long = 1
Const LOGON32_PROVIDER_WINNT40 As Long = 2
Const LOGON32_PROVIDER_WINNT50 As Long = 3


' Influenced from the example at http://aspalliance.com/39
Public Shared Function Login(ByVal uid As String, ByVal pwd As String) As Boolean

    ' Get the user's domain name.
    Dim domainName As String = My.User.Name.Substring(0, My.User.Name.IndexOf("\"))

    ' This token is returned by the LogonUser API call (variable is passed ByRef).
    Dim token As IntPtr

    If LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) Then

        ' Added this line per response to this question:
        WindowsIdentity.Impersonate(token)

        ' If the login succeeds, then impersonate that user by changing CurrentPrincipal.
        Dim wi As New Principal.WindowsIdentity(token)
        Dim wp As New Principal.WindowsPrincipal(wi)

        My.User.CurrentPrincipal = wp
        Return True

    Else
        Return False
    End If

End Function

但是,应用程序使用 .DLL 和连接到 SQL Server 2000 的数据访问层。似乎 SQL Server 在连接字符串中使用“Integrated Security=SSPI”正在接收登录帐户的登录信息进入 Windows 而不是帐户返回 My.User.CurrentPrincipal.Identity,在单步执行代码时,在 WinForms 应用程序代码和 .DLL 的应用程序代码中。

WinForms 应用程序和 .DLL 代码都正确地将 My.User.CurrentPrincipal.Identity 识别为登录到应用程序的帐户,而不是 Windows。它只是没有传播到 SQL Server。这可以通过将 SUSER_SNAME() 写入 T-SQL 中表的列的存储过程来证明。

谁能看出我哪里出错了?

编辑:我已按说明添加了WindowsIdentity.Impersonate(token) 行,但现在当我的 .DLL 尝试创建 SQL Server 连接时,它会引发此错误:

用户“NT AUTHORITY\ANONYMOUS LOGON”登录失败。

【问题讨论】:

你解决了这个问题吗? 【参考方案1】:

您需要拨打WindowsIdentity.Impersonate();:

If LogonUser(...) Then             
   WindowsIdentity.Impersonate(token)

【讨论】:

我添加了上面的行,但我收到了一个异常,正如我在上面编辑的问题中所述。 所以这意味着你现在真的是在冒充。您需要找出模拟用户未通过数据库身份验证的原因。您很可能在不受数据库信任的域中模拟用户(可能是本地用户?)

以上是关于“模拟”用户未传播到 SQL Server 2000的主要内容,如果未能解决你的问题,请参考以下文章

解决JDBC连接SQL_Server未配置集成验证的方法

SQL Server 2012 从 SQL 查询创建用户

sqlserver问题,求思路

最新后缀.*4444后缀勒索病毒文件及SQL Server数据库修复方案

用户“sa”登录失败。用户未与受信任的 SQL Server 连接关联。 (Microsoft SQL Server,错误:18452)在 sql 2008 中

将 .NET 类型映射到 SQL Server 200 数据类型