RegistrySecurity Access is denied. C#

前端 未结 5 1045
说谎
说谎 2021-01-18 11:49

I\'m currently having an issue when writing an app to set permissions on some Legacy keys. Legacy keys are quite locked down and to actually modify them in regedit you have

5条回答
  •  一向
    一向 (楼主)
    2021-01-18 12:38

            RegistryKey rkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Norton\SecurityStatusSDK", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions);
            if (rkey == null)
                throw new Exception("Not Open");
            //-------
            RegistrySecurity _registrySecurity = new RegistrySecurity();//Or rkey.GetAccessControl();
            WindowsIdentity _windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
            RegistryAccessRule _accessRule = new RegistryAccessRule(_windowsIdentity.Name, RegistryRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
            _registrySecurity.AddAccessRule(_accessRule);
            _registrySecurity.SetAccessRuleProtection(false, true);
            try
            {
                rkey.SetAccessControl(_registrySecurity);// <---"Attempted to perform an unauthorized operation."
            }
            catch (UnauthorizedAccessException e)
            {
    
            }
    
            //--------Now, Set owner
    
            _registrySecurity.SetGroup(new NTAccount("Administrators"));  //This is optional
            var SID = new System.Security.Principal.NTAccount("XXX\\Users");
            _registrySecurity.SetOwner(SID);
            rkey.SetAccessControl(_registrySecurity);
    

    I installed Norton Internet Security

提交回复
热议问题