RegistryAccessRule クラス

定義

ユーザーまたはグループに許可されたアクセス権セットまたは拒否されたアクセス権セットを表します。 このクラスは継承できません。

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
継承
RegistryAccessRule
属性

次のコード例では、継承と伝達を使用したアクセス規則を示します。 この例では、 オブジェクトを RegistrySecurity 作成し、 フラグを持つ 2 つの規則を ContainerInherit 作成して追加します。 最初のルールには伝達フラグがありません。2 番目のルールには NoPropagateInheritInheritOnlyがあります。

プログラムは オブジェクトにルールを RegistrySecurity 表示し、 オブジェクトを使用してサブキーを作成します。 プログラムは、子サブキーと孫サブキーを作成し、各サブキーのセキュリティを表示します。 最後に、プログラムはテスト キーを削除します。


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

注釈

クラスはRegistryAccessRule、レジストリ キーの Windows アクセス制御セキュリティを管理するために.NET Frameworkが提供するクラスのセットの 1 つです。 これらのクラスの概要と、基になる Windows アクセス制御構造との関係については、「」を参照してください RegistrySecurity

注意

Windows アクセス制御のセキュリティは、レジストリ キーにのみ適用できます。 キーに格納されている個々のキーと値のペアには適用できません。

レジストリ キーに現在適用されている規則の一覧を取得するには、 メソッドを RegistryKey.GetAccessControl 使用してオブジェクトを RegistrySecurity 取得し、そのメソッドを GetAccessRules 使用してオブジェクトの RegistryAccessRule コレクションを取得します。

RegistryAccessRule オブジェクトは、基になる随意制御アクセス リスト (DACL) 内のアクセス制御エントリと一対一にマップされません。 レジストリ キーのすべてのアクセス規則のセットを取得すると、セットには、すべてのアクセス制御エントリを表すために現在必要なルールの最小数が含まれます。

注意

ルールを適用および削除すると、基になるアクセス制御エントリが変更されます。 可能であれば、アクセス制御エントリの最小数を維持するために、ルール内の情報がマージされます。 したがって、現在のルールの一覧を読むと、追加したすべてのルールの一覧とまったく同じようには見えない場合があります。

オブジェクトを使用して RegistryAccessRule 、ユーザーまたはグループに対して許可または拒否するアクセス権を指定します。 オブジェクトは RegistryAccessRule 常に、許可されたアクセスまたは拒否されたアクセスを表し、両方を表すことはありません。

レジストリ キーにルールを適用するには、 メソッドを RegistryKey.GetAccessControl 使用して オブジェクトを RegistrySecurity 取得します。 オブジェクトを変更するには、 RegistrySecurity そのメソッドを使用して規則を追加し、 メソッドを RegistryKey.SetAccessControl 使用してセキュリティ オブジェクトを再アタッチします。

重要

オブジェクトに対して RegistrySecurity 行った変更は、 メソッドを呼び出 RegistryKey.SetAccessControl して変更されたセキュリティ オブジェクトをレジストリ キーに割り当てるまで、レジストリ キーのアクセス レベルには影響しません。

RegistryAccessRule オブジェクトは不変です。 レジストリ キーのセキュリティは、 クラスの RegistrySecurity メソッドを使用して規則を追加または削除して変更されます。これを行うと、基になるアクセス制御エントリが変更されます。

コンストラクター

RegistryAccessRule(IdentityReference, RegistryRights, AccessControlType)

規則を適用するユーザーまたはグループ、アクセス権、および指定したアクセス権を許可するかどうかを指定して、RegistryAccessRule クラスの新しいインスタンスを初期化します。

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

規則を適用するユーザーまたはグループ、アクセス権、継承フラグ、反映フラグ、および指定したアクセス権を許可するかどうかを指定して、RegistryAccessRule クラスの新しいインスタンスを初期化します。

RegistryAccessRule(String, RegistryRights, AccessControlType)

規則を適用するユーザーまたはグループの名前、アクセス権、および指定したアクセス権を許可するかどうかを指定して、RegistryAccessRule クラスの新しいインスタンスを初期化します。

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

規則を適用するユーザーまたはグループの名前、アクセス権、継承フラグ、反映フラグ、および指定したアクセス権を許可するかどうかを指定して、RegistryAccessRule クラスの新しいインスタンスを初期化します。

プロパティ

AccessControlType

この AccessControlType オブジェクトに関連付けられている AccessRule 値を取得します。

(継承元 AccessRule)
AccessMask

この規則のアクセス マスクを取得します。

(継承元 AuthorizationRule)
IdentityReference

この規則を適用する IdentityReference を取得します。

(継承元 AuthorizationRule)
InheritanceFlags

この規則を子オブジェクトが継承する方法を決定するフラグの値を取得します。

(継承元 AuthorizationRule)
IsInherited

この規則を明示的に設定するか、または親コンテナー オブジェクトから継承するかを指定する値を取得します。

(継承元 AuthorizationRule)
PropagationFlags

反映フラグの値を取得します。このフラグから、この規則を子オブジェクトに反映させる方法を判断します。 このプロパティは、InheritanceFlags 列挙体の値が None でない場合にのみ重要です。

(継承元 AuthorizationRule)
RegistryRights

アクセス規則で許可されたアクセス権または拒否されたアクセス権を取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象