RegistrySecurity.RemoveAccessRule(RegistryAccessRule) Metoda

Definicja

Wyszukuje regułę kontroli dostępu z tym samym użytkownikiem i AccessControlType (zezwalaj lub odrzucaj) jako określoną regułę dostępu oraz z zgodnymi flagami dziedziczenia i propagacji. Jeśli taka reguła zostanie znaleziona, prawa zawarte w określonej regule dostępu zostaną usunięte z niej.

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

Parametry

rule
RegistryAccessRule

Element RegistryAccessRule określający użytkownika i AccessControlType wyszukiwany oraz zestaw flag dziedziczenia i propagacji, z którymi musi być zgodna reguła, jeśli zostanie znaleziona, musi być zgodna. Określa prawa do usunięcia z zgodnej reguły, jeśli zostanie znaleziona.

Zwraca

Boolean

true jeśli zostanie znaleziona zgodna reguła; w przeciwnym razie false.

Wyjątki

rule to null.

Przykłady

Poniższy przykład kodu pokazuje, jak RemoveAccessRule metoda usuwa prawa z zgodnej reguły i jak AddAccessRule metoda scala prawa z zgodnymi regułami.

Przykład tworzy obiekt i dodaje regułę RegistrySecurity , która zezwala na bieżące prawa użytkownika RegistryRights.ReadKey . W tym przykładzie zostanie utworzona reguła, która przyznaje użytkownikowi RegistryRights.SetValue, z tymi samymi prawami dziedziczenia i propagacji co pierwsza reguła, i używa RemoveAccessRule metody , aby usunąć tę nową regułę z RegistrySecurity obiektu. SetValue jest składnikiem elementu ReadKey, więc jest usuwany z zgodnej reguły. Zostaną wyświetlone reguły w RegistrySecurity obiekcie z pozostałymi składnikami elementu ReadKey.

Przykładowy kod wywołuje następnie metodę RemoveAccessRule , aby scalić prawą SetValue kopię z powrotem do reguły w RegistrySecurity obiekcie.

Uwaga

W tym przykładzie obiekt zabezpieczeń nie jest dołączany do RegistryKey obiektu. Drugi przykład w tej sekcji dołącza obiekt zabezpieczeń, a więc wykonaj przykłady w pliku 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
'

Uwagi

Bieżąca RegistrySecurity wartość jest wyszukiwana dla reguły, która ma tego samego użytkownika i tę samą AccessControlType wartość co rule. Jeśli taka reguła nie zostanie znaleziona, nie zostanie podjęta żadna akcja, a metoda zwróci falsewartość . Jeśli zostaną znalezione zgodne reguły, ich flagi dziedziczenia i zgodności są sprawdzane pod kątem zgodności z flagami określonymi w pliku rule. Jeśli nie zostanie znaleziona żadna zgodna reguła, nie zostanie podjęta żadna akcja, a metoda zwróci falsewartość . Jeśli zostanie znaleziona reguła z zgodnymi flagami, prawa określone w rule są usuwane z zgodnej reguły, a metoda zwraca wartość true. Jeśli rule określa prawa, które nie są zawarte w zgodnej regule, żadne działania nie są podejmowane w odniesieniu do tych praw. Jeśli wszystkie prawa zostaną usunięte z zgodnej reguły, cała reguła zostanie usunięta z bieżącego RegistrySecurity obiektu.

Dotyczy