I am creating a winforms document manager. I would like to allow the user to upload to and open documents from a protected network drive.
A specific username and password will be setup for the application so that only the application can access the files.
Normally when I allow the user to open a file I use the OpenFileDialog command. How do I add the username and password?
Thanks.
EDIT
Found some additional code:
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
Credit to the original author, is there any advantages/disadvantages to using this method?