使用用户名和密码打开文件

问题描述

|| 我正在创建一个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
归功于原始作者,使用此方法是否有优点/缺点?     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)