2021年3月17日星期三

Copy file using impersonation and CopyFileEx from Kernel DLL?

I am using the following code to copy a file to a directory where only one X user has access, I am using impersonation but when I use it the CopyFileEx does not work, but I don't know why. if I remove the part of the impersonation it works correctly but I need it to be copied with a user X since in production it has to be like that.

                        ImpersonationUtils impersonation = new ImpersonationUtils();                  var token = impersonation.LogonAsUser("User", "Domain", "pwd");                    if (!IntPtr.Equals(token, IntPtr.Zero))                  {                      System.Security.Principal.WindowsImpersonationContext impersonatedUser = null;                      var newIdentity = new System.Security.Principal.WindowsIdentity(token);                      impersonatedUser = newIdentity.Impersonate();                        bool result = CopyFileEx(filename, tempFilepath, new CopyProgressRoutine(this.CopyProgressHandler), IntPtr.Zero, cancelp, 0);                        if (impersonatedUser != null)                          impersonatedUser.Undo();                        impersonation.LogonAsUserEnd(token);                  }  
https://stackoverflow.com/questions/66684619/copy-file-using-impersonation-and-copyfileex-from-kernel-dll March 18, 2021 at 12:06PM

没有评论:

发表评论