1

Is it possible to run a windows forms app as the logged in user, but make the app save files using different user credentials?

I know the following is incorrect, but what I mean is something along those lines:

var userWithPrivileges = new NetworkCredential(userName, password);
File.Copy(sourceFileName, destFileName, overwrite: true, userWithPrivileges);
Andres A.
  • 1,329
  • 2
  • 21
  • 37

1 Answers1

5

you need the ImpersonateUser call. Note that you can't "just" save as that user, you need to have security access to that user in the first place - or it'd be quite a security leak if anyone could pretend to be anyone else with 2 lines of code!

So you'll need a security access token representing that user, usually obtained through a call to LogonUser, the MSDN link gives sample code.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • 1
    [Here is a good code example](http://stackoverflow.com/questions/2563724/accessing-password-protected-network-drives-in-windows-in-c/2563809#2563809) of how to do it. – Scott Chamberlain Aug 01 '13 at 14:01