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