I have a ASP.NET application, I want access to a share folder, that is located on another server, I explain the situation:
Server A: OS:Windows server 2008; WEB SERVER:IIS7; ASP.NET FRAMEWORK 2.0
Server B: OS:Linux; FOLDER SHARE(service user, password protected)
I tried with the following code:
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
The LogonUserA function return always "false".
How can I solve the problem, essentially I need to signin to the serverB folder.