RegistrySecurity.RemoveAccessRule(RegistryAccessRule) Methode

Definition

Sucht nach einer Zugriffssteuerungsregel mit demselben Benutzer und AccessControlType (zulassen oder verweigern) wie die angegebene Zugriffsregel und mit kompatiblen Vererbungs- und Verteilungsflags. Wenn eine solche Regel gefunden wird, werden die in der angegebenen Zugriffsregel enthaltenen Rechte daraus entfernt.

public:
 bool RemoveAccessRule(System::Security::AccessControl::RegistryAccessRule ^ rule);
public bool RemoveAccessRule(System.Security.AccessControl.RegistryAccessRule rule);
override this.RemoveAccessRule : System.Security.AccessControl.RegistryAccessRule -> bool
Public Function RemoveAccessRule (rule As RegistryAccessRule) As Boolean

Parameter

rule
RegistryAccessRule

A RegistryAccessRule that specifies the user and AccessControlType to search, and a set of vererbung and propagation flags that a matching rule, if found, must be compatible with. Gibt die Rechte an, die aus der kompatiblen Regel entfernt werden sollen, falls gefunden.

Gibt zurück

true wenn eine kompatible Regel gefunden wird; andernfalls false.

Ausnahmen

rule ist null.

Beispiele

Das folgende Codebeispiel zeigt, wie die RemoveAccessRule Methode Rechte aus einer kompatiblen Regel entfernt und wie die AddAccessRule Methode Rechte mit kompatiblen Regeln zusammenführt.

Im Beispiel wird ein RegistrySecurity Objekt erstellt und eine Regel hinzugefügt, die die aktuellen Benutzerrechte RegistryRights.ReadKey zulässt. Anschließend wird eine Regel erstellt, die dem Benutzer RegistryRights.SetValuedie gleichen Vererbungs- und Verteilungsrechte wie die erste Regel gewährt und die RemoveAccessRule Methode verwendet, um diese neue Regel aus dem RegistrySecurity Objekt zu entfernen. SetValue ist ein Bestandteil von ReadKey, sodass er aus der kompatiblen Regel entfernt wird. Die Regeln im RegistrySecurity Objekt werden mit den verbleibenden Bestandteilen des ReadKeyObjekts angezeigt.

Der Beispielcode ruft dann die RemoveAccessRule Methode auf, um die SetValue Rechte wieder mit der Regel im RegistrySecurity Objekt zusammenzuführen.

Note

In diesem Beispiel wird das Sicherheitsobjekt nicht an ein RegistryKey Objekt angefügt. Im zweiten Beispiel in diesem Abschnitt wird ein Sicherheitsobjekt angefügt, und dies tun Sie die Beispiele in RegistryKey.GetAccessControlRegistryKey.SetAccessControl.


using System;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Security;
using Microsoft.Win32;

public class Example
{

    public static void Main()
    {

        string user = Environment.UserDomainName + "\\"
            + Environment.UserName;

        // Create a security object that grants no access.
        RegistrySecurity mSec = new RegistrySecurity();

        // Add a rule that grants the current user ReadKey
        // rights. ReadKey is a combination of four other 
        // rights. The rule is inherited by all 
        // contained subkeys.
        RegistryAccessRule rule = new RegistryAccessRule(user, 
            RegistryRights.ReadKey, 
            InheritanceFlags.ContainerInherit, 
            PropagationFlags.None, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        // Create a rule that allows the current user only the 
        // right to query the key/value pairs of a key, using  
        // the same inheritance and propagation flags as the
        // first rule. QueryValues is a constituent of 
        // ReadKey, so when this rule is removed, using the 
        // RemoveAccessRule method, ReadKey is broken into
        // its constituent parts.
        rule = new RegistryAccessRule(user, 
            RegistryRights.QueryValues, 
            InheritanceFlags.ContainerInherit, 
            PropagationFlags.None, 
            AccessControlType.Allow);
        mSec.RemoveAccessRule(rule);

        // Display the rules in the security object.
        ShowSecurity(mSec);

        // Add the second rule back. It merges with the 
        // existing rule, so that the rule is now displayed
        // as ReadKey.
        mSec.AddAccessRule(rule);

        // Display the rules in the security object.
        ShowSecurity(mSec);
    }

    private static void ShowSecurity(RegistrySecurity security)
    {
        Console.WriteLine("\r\nCurrent access rules:\r\n");

        foreach( RegistryAccessRule ar in security.GetAccessRules(true, true, typeof(NTAccount)) )
        {
            Console.WriteLine("        User: {0}", ar.IdentityReference);
            Console.WriteLine("        Type: {0}", ar.AccessControlType);
            Console.WriteLine("      Rights: {0}", ar.RegistryRights);
            Console.WriteLine(" Inheritance: {0}", ar.InheritanceFlags);
            Console.WriteLine(" Propagation: {0}", ar.PropagationFlags);
            Console.WriteLine("   Inherited? {0}", ar.IsInherited);
            Console.WriteLine();
        }
    }
}

/* This code example produces output similar to following:

Current access rules:

        User: TestDomain\TestUser
        Type: Allow
      Rights: EnumerateSubKeys, Notify, ReadPermissions
 Inheritance: ContainerInherit
 Propagation: None
   Inherited? False


Current access rules:

        User: TestDomain\TestUser
        Type: Allow
      Rights: ReadKey
 Inheritance: ContainerInherit
 Propagation: None
   Inherited? False
 */
Option Explicit
Imports System.Security.AccessControl
Imports System.Security.Principal
Imports System.Security
Imports Microsoft.Win32

Public Class Example

    Public Shared Sub Main()

        Dim user As String = Environment.UserDomainName _ 
            & "\" & Environment.UserName

        ' Create a security object that grants no access.
        Dim mSec As New RegistrySecurity()

        ' Add a rule that grants the current user ReadKey
        ' rights. ReadKey is a combination of four other 
        ' rights. The rule is inherited by all 
        ' contained subkeys.
        Dim rule As New RegistryAccessRule(user, _
            RegistryRights.ReadKey, _
            InheritanceFlags.ContainerInherit, _
            PropagationFlags.None, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        ' Create a rule that allows the current user only the 
        ' right to query the key/value pairs of a key, using  
        ' the same inheritance and propagation flags as the
        ' first rule. QueryValues is a constituent of 
        ' ReadKey, so when this rule is removed, using the 
        ' RemoveAccessRule method, ReadKey is broken into
        ' its constituent parts.
        rule = New RegistryAccessRule(user, _
            RegistryRights.QueryValues, _
            InheritanceFlags.ContainerInherit, _
            PropagationFlags.None, _
            AccessControlType.Allow)
        mSec.RemoveAccessRule(rule)

        ' Display the rules in the security object.
        ShowSecurity(mSec)

        ' Add the second rule back. It merges with the 
        ' existing rule, so that the rule is now displayed
        ' as ReadKey.
        mSec.AddAccessRule(rule)

        ' Display the rules in the security object.
        ShowSecurity(mSec)

    End Sub 

    Private Shared Sub ShowSecurity(ByVal security As RegistrySecurity)
        Console.WriteLine(vbCrLf & "Current access rules:" & vbCrLf)

        For Each ar As RegistryAccessRule In _
            security.GetAccessRules(True, True, GetType(NTAccount))

            Console.WriteLine("        User: {0}", ar.IdentityReference)
            Console.WriteLine("        Type: {0}", ar.AccessControlType)
            Console.WriteLine("      Rights: {0}", ar.RegistryRights)
            Console.WriteLine(" Inheritance: {0}", ar.InheritanceFlags)
            Console.WriteLine(" Propagation: {0}", ar.PropagationFlags)
            Console.WriteLine("   Inherited? {0}", ar.IsInherited)
            Console.WriteLine()
        Next

    End Sub
End Class 

'This code example produces output similar to following:
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: EnumerateSubKeys, Notify, ReadPermissions
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? False
'
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: ReadKey
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? False
'

Hinweise

Der aktuelle RegistrySecurity Wert wird nach einer Regel gesucht, die denselben Benutzer und denselben AccessControlType Wert wie rule. Wenn keine solche Regel gefunden wird, wird keine Aktion ausgeführt, und die Methode wird zurückgegeben false. Wenn übereinstimmende Regeln gefunden werden, werden ihre Vererbungs- und Kompatibilitätskennzeichnungen auf Kompatibilität mit den in ruleder Datei angegebenen Flags überprüft. Wenn keine kompatible Regel gefunden wird, wird keine Aktion ausgeführt, und die Methode wird zurückgegeben false. Wenn eine Regel mit kompatiblen Flags gefunden wird, werden die angegebenen rule Rechte aus der kompatiblen Regel entfernt, und die Methode gibt zurück true. Wenn rule rechte angegeben werden, die nicht in der kompatiblen Regel enthalten sind, wird keine Maßnahme in Bezug auf diese Rechte ergriffen. Wenn alle Rechte aus der kompatiblen Regel entfernt werden, wird die gesamte Regel aus dem aktuellen RegistrySecurity Objekt entfernt.

Gilt für: