VB.NET 网络目录

huangapple go评论77阅读模式
英文:

VB.NET Network Directory

问题

我想使用用户名和密码访问网络上的路径/目录,以便随时查看、复制、删除文件。如何使用凭据访问路径/目录,而不使用用户模拟?

示例
路径:\192.1.2.200\D:\LogFiles
用户名:Admin
密码:Apple

是否有一种方法可以在不使用用户模拟的情况下使用登录凭据来实现这一点?

英文:

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?

答案1

得分: 1

这个主题应该会帮助你:
https://stackoverflow.com/questions/45074628/access-a-directory-in-a-domain-by-username-and-password

与使用全局模拟上下文不同,我们创建一个本地模拟上下文来访问资源,然后在最后销毁它。就好像参数直接传递一样。我们实际上没有其他选择,因为没有办法使用directoryinfo实例传递安全信息。

示例:

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

Public Class testDirAccess

    '模拟功能
    <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

    '文件操作后断开连接
    <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

        '表示经授权的用户帐户的用户令牌
        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
            '使用令牌设置Windows模拟上下文
            Using ctx As WindowsImpersonationContext = (New WindowsIdentity(token)).Impersonate()
                '您的文件操作
                Dim files() As String = Directory.GetFiles("\\remotemachine\share\folder")

                '释放上下文并关闭用户令牌
                ctx.Undo()
                CloseHandle(token)
            End Using
        End If
    End Sub

End Class
英文:

This topic should help you:
https://stackoverflow.com/questions/45074628/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

    &#39;Impersonation functionality
    &lt;DllImport(&quot;advapi32.dll&quot;, SetLastError:=True)&gt;
    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

    &#39;Disconnection after file operations
    &lt;DllImport(&quot;kernel32.dll&quot;)&gt;
    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

        &#39;User token that represents the authorized user account
        Dim token As IntPtr = IntPtr.Zero

        Dim result As Boolean = LogonUser(&quot;username&quot;, &quot;domainname&quot;, &quot;password&quot;, LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, token)

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

                &#39;Release the context, and close user token
                ctx.Undo()
                CloseHandle(token)
            End Using
        End If
    End Sub

End Class

huangapple
  • 本文由 发表于 2023年3月9日 21:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定