Write to a Directory as a Specific User on a Diffe

2019-07-24 11:59发布

问题:

I am wondering, how would one write a file (say for instance, from a Console Application in a scheduled task) to another computer on a different domain? How would one set the Username and Password, such as with the "Log on to..." dialog to authenticate?

Couple of prerequisites exist, obviously:

  • The machines can see each other across the domains, however they need to authenticate
  • You know the login details
  • The username you would log in with has the appropriate permissions to write

Anyone have any idea?

Thanks,

Kyle

回答1:

The approach here is to use Impersonation. Have a look here for a straightforward how-to.

Essentially, you need to obtain a handle to the user token (usually using LogonUser), and then call Impersonate.

There are also several projects (eg this one) to make the process a little easier.