使用C#.NET在Windows上修改注册表权限

时间:2019-04-25 17:57:29

标签: c# .net permissions registry

我正在Windows服务中修改注册表权限,基本上我想做的是

  • 向经过身份验证的用户提供读取权限。
  • 拒绝所有人的写权限。
  • 允许完全控制系统帐户和管理员用户。

这是我的代码,请您查看并告知我,如果我需要更改任何内容,我编写了一个示例应用程序并手动检查了注册表权限。然后,下次我运行我的应用程序时,它没有访问注册表项的权限,甚至没有我赋予已验证用户的读取权限。

            RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyCompany\Somekey",RegistryKeyPermissionCheck.ReadWriteSubTree);

                    RegistryAccessRule rule1 = new RegistryAccessRule(
                                            new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null),
                                            RegistryRights.ReadKey,
                                            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                            PropagationFlags.None,
                                            AccessControlType.Allow);

                    RegistryAccessRule rule2 = new RegistryAccessRule(
                                               new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                                               RegistryRights.WriteKey,
                                               InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                               PropagationFlags.None,
                                               AccessControlType.Deny);

                    RegistryAccessRule rule3 = new RegistryAccessRule(
                                               new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),
                                               RegistryRights.FullControl,
                                               InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                               PropagationFlags.None,
                                               AccessControlType.Allow);


                    RegistryAccessRule rule4 = new RegistryAccessRule(
                                               new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null),
                                               RegistryRights.FullControl,
                                               InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                               PropagationFlags.None,
                                               AccessControlType.Allow);


                    RegistrySecurity registrySecurity = regKey.GetAccessControl();

                    registrySecurity.AddAccessRule(rule1);
                    registrySecurity.AddAccessRule(rule2);
                    registrySecurity.AddAccessRule(rule3);
                    registrySecurity.AddAccessRule(rule4);
                    regKey.SetAccessControl(registrySecurity);

0 个答案:

没有答案