Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

后端 未结 10 1033
走了就别回头了
走了就别回头了 2020-11-22 05:12

We\'ve run into an interesting situation that needs solving, and my searches have turned up nill. I therefore appeal to the SO community for help.

The issue is this:

10条回答
  •  长情又很酷
    2020-11-22 05:41

    im attach my vb.net code based on brian reference

    Imports System.ComponentModel
    
    Imports System.Runtime.InteropServices
    
    Public Class PinvokeWindowsNetworking
    
    Const NO_ERROR As Integer = 0
    
    
    
    Private Structure ErrorClass
    
        Public num As Integer
    
        Public message As String
    
    
    
        Public Sub New(ByVal num As Integer, ByVal message As String)
    
            Me.num = num
    
            Me.message = message
    
        End Sub
    
    End Structure
    
    
    
    Private Shared ERROR_LIST As ErrorClass() = New ErrorClass() {
    
        New ErrorClass(5, "Error: Access Denied"),
    
        New ErrorClass(85, "Error: Already Assigned"),
    
        New ErrorClass(1200, "Error: Bad Device"),
    
        New ErrorClass(67, "Error: Bad Net Name"),
    
        New ErrorClass(1204, "Error: Bad Provider"),
    
        New ErrorClass(1223, "Error: Cancelled"),
    
        New ErrorClass(1208, "Error: Extended Error"),
    
        New ErrorClass(487, "Error: Invalid Address"),
    
        New ErrorClass(87, "Error: Invalid Parameter"),
    
        New ErrorClass(1216, "Error: Invalid Password"),
    
        New ErrorClass(234, "Error: More Data"),
    
        New ErrorClass(259, "Error: No More Items"),
    
        New ErrorClass(1203, "Error: No Net Or Bad Path"),
    
        New ErrorClass(1222, "Error: No Network"),
    
        New ErrorClass(1206, "Error: Bad Profile"),
    
        New ErrorClass(1205, "Error: Cannot Open Profile"),
    
        New ErrorClass(2404, "Error: Device In Use"),
    
        New ErrorClass(2250, "Error: Not Connected"),
    
        New ErrorClass(2401, "Error: Open Files")}
    
    
    
    Private Shared Function getErrorForNumber(ByVal errNum As Integer) As String
    
        For Each er As ErrorClass In ERROR_LIST
    
            If er.num = errNum Then Return er.message
    
        Next
    
    
    
        Try
    
            Throw New Win32Exception(errNum)
    
        Catch ex As Exception
    
            Return "Error: Unknown, " & errNum & " " & ex.Message
    
        End Try
    
    
    
        Return "Error: Unknown, " & errNum
    
    End Function
    
    
    
    
    
    Private Shared Function WNetUseConnection(ByVal hwndOwner As IntPtr, ByVal lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserID As String, ByVal dwFlags As Integer, ByVal lpAccessName As String, ByVal lpBufferSize As String, ByVal lpResult As String) As Integer
    
    End Function
    
    
    
    
    
    Private Shared Function WNetCancelConnection2(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Boolean) As Integer
    
    End Function
    
    
    
    
    
    Private Class NETRESOURCE
    
        Public dwScope As Integer = 0
    
        Public dwType As Integer = 0
    
        Public dwDisplayType As Integer = 0
    
        Public dwUsage As Integer = 0
    
        Public lpLocalName As String = ""
    
        Public lpRemoteName As String = ""
    
        Public lpComment As String = ""
    
        Public lpProvider As String = ""
    
    End Class
    
    
    
    Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String) As String
    
        Return connectToRemote(remoteUNC, username, password, False)
    
    End Function
    
    
    
    Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String, ByVal promptUser As Boolean) As String
    
        Dim nr As NETRESOURCE = New NETRESOURCE()
    
        nr.dwType = ResourceTypes.Disk
    
        nr.lpRemoteName = remoteUNC
    
        Dim ret As Integer
    
    
    
        If promptUser Then
    
            ret = WNetUseConnection(IntPtr.Zero, nr, "", "", Connects.Interactive Or Connects.Prompt, Nothing, Nothing, Nothing)
    
        Else
    
            ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, Nothing, Nothing, Nothing)
    
        End If
    
    
    
        If ret = NO_ERROR Then Return Nothing
    
        Return getErrorForNumber(ret)
    
    End Function
    
    
    
    Public Shared Function disconnectRemote(ByVal remoteUNC As String) As String
    
        Dim ret As Integer = WNetCancelConnection2(remoteUNC, Connects.UpdateProfile, False)
    
        If ret = NO_ERROR Then Return Nothing
    
        Return getErrorForNumber(ret)
    
    End Function
    
    
    Enum Resources As Integer
    
        Connected = &H1
    
        GlobalNet = &H2
    
        Remembered = &H3
    
    End Enum
    
    
    Enum ResourceTypes As Integer
    
        Any = &H0
    
        Disk = &H1
    
        Print = &H2
    
    End Enum
    
    
    Enum ResourceDisplayTypes As Integer
    
        Generic = &H0
    
        Domain = &H1
    
        Server = &H2
    
        Share = &H3
    
        File = &H4
    
        Group = &H5
    
    End Enum
    
    
    Enum ResourceUsages As Integer
    
        Connectable = &H1
    
        Container = &H2
    
    End Enum
    
    
    Enum Connects As Integer
    
        Interactive = &H8
    
        Prompt = &H10
    
        Redirect = &H80
    
        UpdateProfile = &H1
    
        CommandLine = &H800
    
        CmdSaveCred = &H1000
    
        LocalDrive = &H100
    
    End Enum
    
    
    End Class
    

    how to use it

    Dim login = PinvokeWindowsNetworking.connectToRemote("\\ComputerName", "ComputerName\UserName", "Password")
    
        If IsNothing(login) Then
    
    
    
            'do your thing on the shared folder
    
    
    
           PinvokeWindowsNetworking.disconnectRemote("\\ComputerName")
    
        End If
    

提交回复
热议问题