我正在创建一个winforms文档管理器。我想允许用户上传到受保护的网络驱动器并从中打开文档。
将为应用程序设置特定的用户名和密码,以便只有应用程序才能访问这些文件。
通常,当我允许用户打开文件时,我使用OpenFileDialog命令。如何添加用户名和密码?
感谢。
修改
找到一些额外的代码:
Public Class ImpersonateUser
Private Declare Auto Function LogonUser Lib "advapi32.dll" ( _
ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Int32, _
ByVal dwLogonProvider As Int32, _
ByRef phToken As IntPtr _
) As Int32
Private Declare Auto Function ImpersonateLoggedOnUser Lib "advapi32.dll" ( _
ByVal hToken As IntPtr _
) As Int32
Declare Auto Function RevertToSelf Lib "advapi32.dll" ( _
) As Int32
Private Function ImpersonateValidUser( _
ByVal Username As String, _
ByVal Domain As String, _
ByVal Password As String _
) As Boolean
Dim LogonType As Int32
Dim LogonProvider As Int32
Dim Tk As IntPtr
LogonType = 2 ' Interactive.
LogonProvider = 0 ' Default Provider.
If LogonUser(Username, Domain, Password, LogonType, LogonProvider, Tk) <> 0 Then
Return (ImpersonateLoggedOnUser(Tk) <> 0)
End If
Return False
End Function
Private Sub UndoImpersonation()
RevertToSelf()
End Sub
Sub test()
If ImpersonateValidUser("accountname", "Domainname", "password") Then
' This code runs unter the privileges of the impersonated user.
Process.Start("C:\foo.exe")
UndoImpersonation()
End If
End Sub
End Class
归功于原作者,使用这种方法有什么优点/缺点吗?