0

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.

invictus1306
  • 587
  • 1
  • 4
  • 19
  • All I can say instantly is, the [documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/aa378184%28v=vs.85%29.aspx) says "You cannot use LogonUser to log on to a remote computer." Is this perhaps a ActiveDirectory/LDAP environment? – Alexander Jun 14 '13 at 13:45
  • Hi Alexander, thanks for your answer. I'm not sure for ActiveDirectory/LDAP environment, but if you need know can ask. Regardless of my method, I need, access from code to some files in the share folder, this folder is located on another server, and is protected by username and password. For share folder is used netapp Do you know any method that can help me solve the problem? – invictus1306 Jun 17 '13 at 07:42

0 Answers0