0

I would like to access a path/directory on the network with a username and password so that I can access it anytime to check, copy, delete files. How do I access the path/directory using the credentials and not use the user impersonate?

Example Path: \192.1.2.200\D:\LogFiles
Username: Admin Password: Apple

Is there any way to do this without the user impersonation and actually using the login credentials?

sophia
  • 3
  • 4

1 Answers1

1

This topic should help you: access a directory in a domain by username and password

Instead of using the global impersonation context, we create a local one to access the resource, then destroyed it at the end. Exactly as if the parameters were transmitted directly. We don't really have alternative as there is no way to transmit security information with the directoryinfo instance.

example :

Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security.Principal

Public Class testDirAccess

    'Impersonation functionality
    <DllImport("advapi32.dll", SetLastError:=True)>
    Private Shared Function LogonUser(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
    End Function

    'Disconnection after file operations
    <DllImport("kernel32.dll")>
    Private Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
    End Function

    Sub test()

        Const LOGON_TYPE_NEW_CREDENTIALS As Integer = 9
        Const LOGON32_PROVIDER_WINNT50 As Integer = 3

        'User token that represents the authorized user account
        Dim token As IntPtr = IntPtr.Zero

        Dim result As Boolean = LogonUser("username", "domainname", "password", LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, token)

        If result = True Then
            'Use token to setup a WindowsImpersonationContext 
            Using ctx As WindowsImpersonationContext = (New WindowsIdentity(token)).Impersonate()
                'Your file operations
                Dim files() As String = Directory.GetFiles("\\remotemachine\share\folder")

                'Release the context, and close user token
                ctx.Undo()
                CloseHandle(token)
            End Using
        End If
    End Sub

End Class
tuyau2poil
  • 767
  • 1
  • 2
  • 7
  • Isnt it the user impersonation? i would like to access using the real credentials and without impersonating.. – sophia Mar 12 '23 at 17:20
  • If the current user in session has necessary permissions, he can access the resource directly because the request will be automatically accompanied by the user's security token. In this case you can use Dim files() As String = Directory.GetFiles("\\remotemachine\share\folder") directly without all stuff in answer. – tuyau2poil Mar 12 '23 at 18:01
  • i need to add my software to a different PC and access from it. thats why will need to add a username and password to access the network drive folder path. – sophia Mar 21 '23 at 16:23
  • This is what I thought: in this case use my solution by replacing "Username", "Domainname", "Password" by your identifiers. Look at the line `Dim result As Boolean = LogonUser("username", "domainname", "password", LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, token)` You can also put textboxs on a Form to enter them manually. What bothers you in this answer? This is exactly what you need .... – tuyau2poil Mar 21 '23 at 17:13