Share via


RegistryAccessRule Třída

Definice

Představuje sadu přístupových práv povolených nebo zakázaných pro uživatele nebo skupinu. Tato třída se nemůže dědit.

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
Dědičnost
RegistryAccessRule
Atributy

Příklady

Následující příklad kódu ukazuje pravidla přístupu s dědičností a šířením. Příklad vytvoří RegistrySecurity objekt, pak vytvoří a přidá dvě pravidla, která mají ContainerInherit příznak. První pravidlo nemá žádné příznaky šíření, zatímco druhé má NoPropagateInherit a InheritOnly.

Program zobrazí pravidla v objektu RegistrySecurity a pak použije objekt k vytvoření podklíče. Program vytvoří podřízený podklíč a podklíč s vnoučaty a pak zobrazí zabezpečení pro každý podklíč. Nakonec program odstraní testovací klíče.


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

Poznámky

Třída RegistryAccessRule je jednou ze sady tříd, které rozhraní .NET Framework poskytuje pro správu zabezpečení řízení přístupu systému Windows k klíčům registru. Přehled těchto tříd a jejich vztahu k základním strukturám řízení přístupu systému Windows najdete v tématu RegistrySecurity.

Poznámka

Zabezpečení řízení přístupu systému Windows je možné použít pouze pro klíče registru. Nelze ho použít pro jednotlivé páry klíč/hodnota uložené v klíči.

Chcete-li získat seznam pravidel aktuálně použitých na klíč registru, použijte metodu RegistryKey.GetAccessControl k získání objektu RegistrySecurity a pak pomocí jeho GetAccessRules metody získejte kolekci RegistryAccessRule objektů.

RegistryAccessRule objekty nemapují 1:1 s položkami řízení přístupu v podkladovém volitelném seznamu řízení přístupu (DACL). Když získáte sadu všech pravidel přístupu pro klíč registru, sada obsahuje minimální počet pravidel aktuálně potřebných k vyjádření všech položek řízení přístupu.

Poznámka

Základní položky řízení přístupu se mění při použití a odebírání pravidel. Informace v pravidlech se pokud možno sloučí, aby se zachoval co nejmenší počet položek řízení přístupu. Když tedy čtete aktuální seznam pravidel, nemusí vypadat přesně jako seznam všech pravidel, která jste přidali.

Pomocí RegistryAccessRule objektů můžete zadat přístupová práva, která chcete uživateli nebo skupině povolit nebo odepřít. Objekt RegistryAccessRule vždy představuje povolený nebo zakázaný přístup, nikdy obojí.

Pokud chcete použít pravidlo pro klíč registru, použijte k získání objektu RegistryKey.GetAccessControl metodu RegistrySecurity . RegistrySecurity Upravte objekt pomocí jeho metod pro přidání pravidla a pak použijte metodu RegistryKey.SetAccessControl k opětovnému připojení objektu zabezpečení.

Důležité

Změny provedené v objektu RegistrySecurity nemají vliv na úrovně přístupu klíče registru, dokud nezavoláte metodu RegistryKey.SetAccessControl pro přiřazení změněného objektu zabezpečení ke klíči registru.

RegistryAccessRule objekty jsou neměnné. Zabezpečení klíče registru se mění pomocí metod RegistrySecurity třídy pro přidání nebo odebrání pravidel. Při tom se upraví podkladové položky řízení přístupu.

Konstruktory

RegistryAccessRule(IdentityReference, RegistryRights, AccessControlType)

Inicializuje novou instanci RegistryAccessRule třídy s určením uživatele nebo skupiny, na které se pravidlo vztahuje, přístupová práva a zda jsou zadaná přístupová práva povolena nebo odepřena.

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

Inicializuje novou instanci RegistryAccessRule třídy s určením uživatele nebo skupiny, na které se pravidlo vztahuje, přístupových práv, příznaků dědičnosti, příznaků šíření a toho, zda jsou zadaná přístupová práva povolena nebo odepřena.

RegistryAccessRule(String, RegistryRights, AccessControlType)

Inicializuje novou instanci RegistryAccessRule třídy a určí název uživatele nebo skupiny, na které se pravidlo vztahuje, přístupová práva a zda jsou zadaná přístupová práva povolena nebo odepřena.

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

Inicializuje novou instanci RegistryAccessRule třídy a určí název uživatele nebo skupiny, na které se pravidlo vztahuje, přístupová práva, příznaky dědičnosti, příznaky šíření a zda jsou zadaná přístupová práva povolena nebo odepřena.

Vlastnosti

AccessControlType

Získá hodnotu přidruženou AccessControlType k tomuto AccessRule objektu.

(Zděděno od AccessRule)
AccessMask

Získá masku přístupu pro toto pravidlo.

(Zděděno od AuthorizationRule)
IdentityReference

Získá, IdentityReference na který se toto pravidlo vztahuje.

(Zděděno od AuthorizationRule)
InheritanceFlags

Získá hodnotu příznaků, které určují, jak je toto pravidlo zděděno podřízenými objekty.

(Zděděno od AuthorizationRule)
IsInherited

Získá hodnotu označující, zda je toto pravidlo explicitně nastaveno nebo je zděděno z nadřazeného objektu kontejneru.

(Zděděno od AuthorizationRule)
PropagationFlags

Získá hodnotu příznaků šíření, které určují, jak se dědičnost tohoto pravidla šíří do podřízených objektů. Tato vlastnost je významná pouze v případě, že hodnota výčtu InheritanceFlags není None.

(Zděděno od AuthorizationRule)
RegistryRights

Získá práva povolená nebo odepřená pravidlem přístupu.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro