微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用用户名和密码打开文件

|| 我正在创建一个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 举报,一经查实,本站将立刻删除。