How to copy a file from UNC-share to the local system?

I am stuck on this issue.

I have a UNC share, I know account information that has full access, but it does not have access to my local system. I can access the remote UNC with:

var token = default(IntPtr); var context = default(WindowsImpersonationContext); LogonUser(_config.Username, _config.Domain, _config.Password, 2, 0, out token); context = WindowsIdentity.Impersonate(token); //TODO :: System.IO operations File.Copy("remote-unc-path","local-path",true); // Exception : Access is denied. context.Undo(); CloseHandle(token); 

But I can’t access my local system during the impersonation because the account does not have access to it.

How to copy a file in this situation? Do I need to use something like a buffer and enable / disable impersonation?

+6
source share
1 answer

What you need to do is read all the bytes and then write them:

  var token = default(IntPtr); using (var context = default(WindowsImpersonationContext)) { LogonUser(_config.Username, _config.Domain, _config.Password, 2, 0, out token); context = WindowsIdentity.Impersonate(token); var bytes = File.ReadAllBytes("remote-unc-path"); context.Undo(); CloseHandle(token); File.WriteAllBytes("local-path", bytes); } 
+3
source

Source: https://habr.com/ru/post/891556/


All Articles