0

我有一个 ASP.NET 应用程序,我想访问位于另一台服务器上的共享文件夹,我解释一下情况:
服务器 A:操作系统:Windows server 2008;网络服务器:IIS7;ASP.NET FRAMEWORK 2.0
服务器 B:操作系统:Linux;文件夹共享(服务用户,密码保护)我尝试使用以下代码:

Dim impersonationContext As WindowsImpersonationContext

Declare Function LogonUserA 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 Integer

Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                        ByVal ExistingTokenHandle As IntPtr, _
                        ByVal ImpersonationLevel As Integer, _
                        ByRef DuplicateTokenHandle As IntPtr) As Integer

Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

Private Function impersonateValidUser(ByVal userName As String, _
ByVal domain As String, ByVal password As String) As Boolean

    Dim tempWindowsIdentity As WindowsIdentity
    Dim token As IntPtr = IntPtr.Zero
    Dim tokenDuplicate As IntPtr = IntPtr.Zero
    impersonateValidUser = False
    Dim _impersonatedUser As System.Security.Principal.WindowsImpersonationContext

    Const LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9
    Const LOGON32_PROVIDER_WINNT50 As Integer = 3
    Const SecurityImpersonation As Integer = 2

    Dim win32ErrorNumber As Integer
    Dim _tokenHandle As New IntPtr(0)
    Dim _dupeTokenHandle As New IntPtr(0)

    _tokenHandle = IntPtr.Zero
    _dupeTokenHandle = IntPtr.Zero

    If RevertToSelf() Then
        If Not LogonUserA(userName, domain, password, 2, 0, token) Then
            win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
            _Alert("ERROR NUMBER: " + win32ErrorNumber.ToString)
        Else
            If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                _Alert("token: " + token.ToString)
                _Alert("token Duplciate: " + tokenDuplicate.ToString)
                tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
                impersonationContext = tempWindowsIdentity.Impersonate()
                If Not impersonationContext Is Nothing Then
                    impersonateValidUser = True
                End If
            End If
        End If
    End If
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then
        CloseHandle(tokenDuplicate)
    End If
    If Not token.Equals(IntPtr.Zero) Then
        CloseHandle(token)
    End If
End Function

LogonUserA 函数总是返回“false”。
我该如何解决这个问题,基本上我需要登录到 serverB 文件夹。

4

0 回答 0