How to set a registry access rule on a remote computer using C #

I am trying to establish a registry access rule on a remote computer:

using (RegistryKey localMachineKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, serverName)) { RegistrySecurity rs = new RegistrySecurity(); rs.AddAccessRule(new RegistryAccessRule(userName, RegistryRights.FullControl, AccessControlType.Allow)); using (RegistryKey subKey = localMachineKey.CreateSubKey(registryKey)) { subKey.SetValue(name, value); subKey.SetAccessControl(rs); } } 

this leads to the following exception:

  System.NotSupportedException: The supplied handle is invalid. This can happen when trying to set an ACL on an anonymous kernel object. at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext) at System.Security.AccessControl.NativeObjectSecurity.Persist(SafeHandle handle, AccessControlSections includeSections, Object exceptionContext) at System.Security.AccessControl.RegistrySecurity.Persist(SafeRegistryHandle hKey, String keyName)... 

Does anyone know how to make this work? Thanks!

+4
source share
1 answer

Using WinRM may be an option. How to access WinRM in C #

This link assumes that along with some additional information:

http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/0beee366-ee8d-4052-b1b9-8ad9bf0f8ff0/

Part of the link suggests that this cannot be installed remotely. However, below, Shaka_01 mentions call.SetAccessRuleProtection.

 RegistryKey rk = RegistryKey.OpenRemoteBaseKey(...); RegistrySecurity rs = rk.GetAccessControl(AccessControlSections.All); rs.SetAccessRuleProtection(true, true); //this line you need to set ACL on a remote machines registry key. 
+2
source

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


All Articles