RegistryAccessRule Klasa

Definicja

Reprezentuje zestaw praw dostępu dozwolonych lub odrzuconych dla użytkownika lub grupy. Klasa ta nie może być dziedziczona.

public ref class RegistryAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class RegistryAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class RegistryAccessRule : System.Security.AccessControl.AccessRule
type RegistryAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type RegistryAccessRule = class
    inherit AccessRule
Public NotInheritable Class RegistryAccessRule
Inherits AccessRule
Dziedziczenie
RegistryAccessRule
Atrybuty

Przykłady

Poniższy przykład kodu przedstawia reguły dostępu z dziedziczeniem i propagacją. Przykład tworzy RegistrySecurity obiekt, a następnie tworzy i dodaje dwie reguły, które mają flagę ContainerInherit . Pierwsza reguła nie ma flag propagacji, natomiast druga ma NoPropagateInherit flagi i InheritOnly.

Program wyświetla reguły w RegistrySecurity obiekcie, a następnie używa obiektu do utworzenia podklucza. Program tworzy podrzędny podklucz i podklucz wnuka, a następnie wyświetla zabezpieczenia dla każdego podklucza. Na koniec program usuwa klucze testowe.


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

public class Example
{
    public static void Main()
    {
        const string TestKey = "TestKey3927";
        RegistryKey cu = Registry.CurrentUser;

        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 the right
        // to read and enumerate the name/value pairs in a key, 
        // to read its access and audit rules, to enumerate
        // its subkeys, to create subkeys, and to delete the key. 
        // The rule is inherited by all contained subkeys.
        //
        RegistryAccessRule rule = new RegistryAccessRule(user, 
           RegistryRights.ReadKey | RegistryRights.WriteKey 
               | RegistryRights.Delete, 
           InheritanceFlags.ContainerInherit, 
           PropagationFlags.None, 
           AccessControlType.Allow
        );
        mSec.AddAccessRule(rule);

        // Add a rule that allows the current user the right
        // right to set the name/value pairs in a key. 
        // This rule is inherited by contained subkeys, but
        // propagation flags limit it to immediate child 
        // subkeys.
        rule = new RegistryAccessRule(user, 
            RegistryRights.ChangePermissions, 
            InheritanceFlags.ContainerInherit, 
            PropagationFlags.InheritOnly | 
                PropagationFlags.NoPropagateInherit, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

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

        // Create the test key using the security object.
        //
        RegistryKey rk = cu.CreateSubKey(TestKey, 
            RegistryKeyPermissionCheck.ReadWriteSubTree, mSec);

        // Create a child subkey and a grandchild subkey, 
        // without security.
        RegistryKey rkChild = rk.CreateSubKey("ChildKey", 
            RegistryKeyPermissionCheck.ReadWriteSubTree);
        RegistryKey rkGrandChild = 
            rkChild.CreateSubKey("GrandChildKey", 
                RegistryKeyPermissionCheck.ReadWriteSubTree);

        Show(rk);
        Show(rkChild);
        Show(rkGrandChild);

        rkGrandChild.Close();
        rkChild.Close();
        rk.Close();

        cu.DeleteSubKeyTree(TestKey);
    }

    private static void Show(RegistryKey rk)
    {
        Console.WriteLine(rk.Name);
        ShowSecurity(rk.GetAccessControl());
    }

    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: SetValue, CreateSubKey, Delete, ReadKey
 Inheritance: ContainerInherit
 Propagation: None
   Inherited? False

        User: TestDomain\TestUser
        Type: Allow
      Rights: ChangePermissions
 Inheritance: ContainerInherit
 Propagation: NoPropagateInherit, InheritOnly
   Inherited? False

HKEY_CURRENT_USER\TestKey3927

Current access rules:

        User: TestDomain\TestUser
        Type: Allow
      Rights: SetValue, CreateSubKey, Delete, ReadKey
 Inheritance: ContainerInherit
 Propagation: None
   Inherited? False

        User: TestDomain\TestUser
        Type: Allow
      Rights: ChangePermissions
 Inheritance: ContainerInherit
 Propagation: NoPropagateInherit, InheritOnly
   Inherited? False

HKEY_CURRENT_USER\TestKey3927\ChildKey

Current access rules:

        User: TestDomain\TestUser
        Type: Allow
      Rights: SetValue, CreateSubKey, Delete, ReadKey
 Inheritance: ContainerInherit
 Propagation: None
   Inherited? True

        User: TestDomain\TestUser
        Type: Allow
      Rights: ChangePermissions
 Inheritance: None
 Propagation: None
   Inherited? True

HKEY_CURRENT_USER\TestKey3927\ChildKey\GrandChildKey

Current access rules:

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

Public Class Example

    Public Shared Sub Main()

        Const TestKey As String = "TestKey3927"
        Dim cu As RegistryKey = Registry.CurrentUser

        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 the right
        ' to read and enumerate the name/value pairs in a key, 
        ' to read its access and audit rules, to enumerate
        ' its subkeys, to create subkeys, and to delete the key. 
        ' The rule is inherited by all contained subkeys.
        '
        Dim rule As New RegistryAccessRule(user, _
            RegistryRights.ReadKey Or RegistryRights.WriteKey _
                Or RegistryRights.Delete, _
            InheritanceFlags.ContainerInherit, _
            PropagationFlags.None, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        ' Add a rule that allows the current user the right
        ' right to set the name/value pairs in a key. 
        ' This rule is inherited by contained subkeys, but
        ' propagation flags limit it to immediate child 
        ' subkeys.
        rule = New RegistryAccessRule(user, _
            RegistryRights.ChangePermissions, _
            InheritanceFlags.ContainerInherit, _
            PropagationFlags.InheritOnly Or PropagationFlags.NoPropagateInherit, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

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

        ' Create the test key using the security object.
        '
        Dim rk As RegistryKey = cu.CreateSubKey(TestKey, _
            RegistryKeyPermissionCheck.ReadWriteSubTree, _
            mSec)

        ' Create a child subkey and a grandchild subkey, 
        ' without security.
        Dim rkChild As RegistryKey= rk.CreateSubKey("ChildKey", _
            RegistryKeyPermissionCheck.ReadWriteSubTree)
        Dim rkGrandChild As RegistryKey = _
            rkChild.CreateSubKey("GrandChildKey", _
                RegistryKeyPermissionCheck.ReadWriteSubTree)

        Show(rk)
        Show(rkChild)
        Show(rkGrandChild)

        rkGrandChild.Close()
        rkChild.Close()
        rk.Close()

        cu.DeleteSubKeyTree(TestKey)
    End Sub 

    Private Shared Sub Show(ByVal rk As RegistryKey)
        Console.WriteLine(rk.Name)            
        ShowSecurity(rk.GetAccessControl())
    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: SetValue, CreateSubKey, Delete, ReadKey
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? False
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: ChangePermissions
' Inheritance: ContainerInherit
' Propagation: NoPropagateInherit, InheritOnly
'   Inherited? False
'
'HKEY_CURRENT_USER\TestKey3927
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: SetValue, CreateSubKey, Delete, ReadKey
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? False
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: ChangePermissions
' Inheritance: ContainerInherit
' Propagation: NoPropagateInherit, InheritOnly
'   Inherited? False
'
'HKEY_CURRENT_USER\TestKey3927\ChildKey
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: SetValue, CreateSubKey, Delete, ReadKey
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? True
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: ChangePermissions
' Inheritance: None
' Propagation: None
'   Inherited? True
'
'HKEY_CURRENT_USER\TestKey3927\ChildKey\GrandChildKey
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: SetValue, CreateSubKey, Delete, ReadKey
' Inheritance: ContainerInherit
' Propagation: None
'   Inherited? True

Uwagi

Klasa RegistryAccessRule jest jednym z zestawów klas, które .NET Framework zapewnia do zarządzania zabezpieczeniami kontroli dostępu systemu Windows w kluczach rejestru. Aby zapoznać się z omówieniem tych klas i ich relacji z podstawowymi strukturami kontroli dostępu systemu Windows, zobacz RegistrySecurity.

Uwaga

Zabezpieczenia kontroli dostępu systemu Windows można stosować tylko do kluczy rejestru. Nie można go zastosować do pojedynczych par klucz/wartość przechowywanych w kluczu.

Aby uzyskać listę reguł stosowanych obecnie do klucza rejestru, użyj RegistryKey.GetAccessControl metody , aby uzyskać RegistrySecurity obiekt, a następnie użyj jego GetAccessRules metody w celu uzyskania kolekcji RegistryAccessRule obiektów.

RegistryAccessRule obiekty nie mapują "jeden do jednego" z wpisami kontroli dostępu w podstawowej uznaniowej liście dostępu kontroli dostępu (DACL). Po pobraniu zestawu wszystkich reguł dostępu dla klucza rejestru zestaw zawiera minimalną liczbę reguł, które są obecnie wymagane do wyrażenia wszystkich wpisów kontroli dostępu.

Uwaga

Podstawowe wpisy kontroli dostępu zmieniają się w miarę stosowania i usuwania reguł. Informacje w regułach są scalane, jeśli to możliwe, aby zachować najmniejszą liczbę wpisów kontroli dostępu. W związku z tym podczas odczytywania bieżącej listy reguł może ona nie wyglądać dokładnie tak jak lista wszystkich dodanych reguł.

Użyj RegistryAccessRule obiektów, aby określić prawa dostępu, aby zezwolić lub odmówić użytkownikowi lub grupie. RegistryAccessRule Obiekt zawsze reprezentuje dozwolony dostęp lub odmowa dostępu, nigdy nie oba te elementy.

Aby zastosować regułę do klucza rejestru, użyj RegistryKey.GetAccessControl metody w celu pobrania RegistrySecurity obiektu. Zmodyfikuj RegistrySecurity obiekt przy użyciu jego metod, aby dodać regułę, a następnie użyj RegistryKey.SetAccessControl metody , aby ponownie dołączyć obiekt zabezpieczeń.

Ważne

Zmiany wprowadzone w RegistrySecurity obiekcie nie mają wpływu na poziomy dostępu klucza rejestru do momentu wywołania RegistryKey.SetAccessControl metody w celu przypisania zmienionego obiektu zabezpieczeń do klucza rejestru.

RegistryAccessRule obiekty są niezmienne. Zabezpieczenia klucza rejestru są modyfikowane przy użyciu metod RegistrySecurity klasy w celu dodawania lub usuwania reguł. W ten sposób podstawowe wpisy kontroli dostępu są modyfikowane.

Konstruktory

RegistryAccessRule(IdentityReference, RegistryRights, AccessControlType)

Inicjuje RegistryAccessRule nowe wystąpienie klasy, określając użytkownika lub grupę, do których ma zastosowanie reguła, prawa dostępu oraz czy określone prawa dostępu są dozwolone, czy blokowane.

RegistryAccessRule(IdentityReference, RegistryRights, InheritanceFlags, PropagationFlags, AccessControlType)

Inicjuje nowe wystąpienie RegistryAccessRule klasy, określając użytkownika lub grupę, do których ma zastosowanie reguła, prawa dostępu, flagi dziedziczenia, flagi propagacji i czy określone prawa dostępu są dozwolone, czy blokowane.

RegistryAccessRule(String, RegistryRights, AccessControlType)

Inicjuje RegistryAccessRule nowe wystąpienie klasy, określając nazwę użytkownika lub grupy, do których ma zastosowanie reguła, prawa dostępu oraz czy określone prawa dostępu są dozwolone, czy blokowane.

RegistryAccessRule(String, RegistryRights, InheritanceFlags, PropagationFlags, AccessControlType)

Inicjuje nowe wystąpienie RegistryAccessRule klasy, określając nazwę użytkownika lub grupy, do których ma zastosowanie reguła, prawa dostępu, flagi dziedziczenia, flagi propagacji i czy określone prawa dostępu są dozwolone, czy blokowane.

Właściwości

AccessControlType

AccessControlType Pobiera wartość skojarzona z tym AccessRule obiektem.

(Odziedziczone po AccessRule)
AccessMask

Pobiera maskę dostępu dla tej reguły.

(Odziedziczone po AuthorizationRule)
IdentityReference

Pobiera regułę IdentityReference , do której ma zastosowanie ta reguła.

(Odziedziczone po AuthorizationRule)
InheritanceFlags

Pobiera wartość flag, które określają, jak ta reguła jest dziedziczona przez obiekty podrzędne.

(Odziedziczone po AuthorizationRule)
IsInherited

Pobiera wartość wskazującą, czy ta reguła jest jawnie ustawiona, czy dziedziczona z nadrzędnego obiektu kontenera.

(Odziedziczone po AuthorizationRule)
PropagationFlags

Pobiera wartość flag propagacji, które określają, jak dziedziczenie tej reguły jest propagowane do obiektów podrzędnych. Ta właściwość jest znacząca tylko wtedy, gdy wartość InheritanceFlags wyliczenia nie Nonejest .

(Odziedziczone po AuthorizationRule)
RegistryRights

Pobiera prawa dozwolone lub odrzucane przez regułę dostępu.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy