Exemplo n.º 1
0
        private FileSecurity GetFileSecurity(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (!_fileIoProvider.Exists(fileName))
            {
                throw new EntityNotFoundException("Could not find file to set ownership.", fileName);
            }

            // Allow this process to circumvent ACL restrictions
            _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeRestorePrivilege, true);

            // Sometimes this is required and other times it works without it. Not sure when.
            _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeTakeOwnershipPrivilege, true);

            var accessControl = _fileIoProvider.GetAccessControl(fileName);

            return(accessControl);
        }
Exemplo n.º 2
0
 private void TakeOwnership(string registryKey)
 {
     try
     {
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeRestorePrivilege, true);
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeBackupPrivilege, true);
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeTakeOwnershipPrivilege, true);
         _registryProvider.TakeLocalMachineOwnership(registryKey, _userProvider.GetCurrentUser());
     }
     finally
     {
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeRestorePrivilege, false);
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeBackupPrivilege, false);
         _windowsApiProvider.ModifyPrivilege(Enums.WindowsApiPrivelegeNames.SeTakeOwnershipPrivilege, false);
     }
 }