I need to copy file from user temp folder under impersonated user. But sometimes it works and sometimes doesn’t.
For example if virual machine is multiuser it may not work for some users. If virtual machine is single user it works.
What may be the case?
My process looks like:
-
Under logged user I get his Temp Folder.
-
In this Temp folder I create Common Random Folder. On that folder I give permisssion for impersonated user by that function:
function SetDocumentBufferFolderPermission(const path, user: string): Boolean;
begin
Result := False;
if AddAceToObjectsSecurityDescriptor(PWideChar(path), SE_FILE_OBJECT, PWideChar(user), TRUSTEE_IS_NAME,
GENERIC_ALL,
GRANT_ACCESS,
SUB_CONTAINERS_AND_OBJECTS_INHERIT) = NOERROR then
Result := True;
end;
- Then in that Common Random Folder I create another Random Folder and copy file into it.
- Then I impersonamate user and try to copy this file to another place. In some cases it is successfull in some cases it is not. Error is – Access is denied
I added logs to check every part of path – on that parts impesonated user have access (checking by DirectoryExists function). The Impersonated user have right on all folders except C:Users<UserName>~1. But that rights is the same on users where my procedure works and where doesn’t.