英文:
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
'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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论