RegistryAccessRuleとは? わかりやすく解説

RegistryAccessRule クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Security.AccessControl
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public NotInheritable Class
 RegistryAccessRule
    Inherits AccessRule
Dim instance As RegistryAccessRule
public sealed class RegistryAccessRule : AccessRule
public ref class RegistryAccessRule sealed
 : public AccessRule
public final class RegistryAccessRule extends
 AccessRule
public final class RegistryAccessRule extends
 AccessRule
解説解説

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

メモメモ

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

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

RegistryAccessRule オブジェクトは、基になる随意アクセス制御リスト (DACL: Discretionary Access Control List) のアクセス制御エントリと一対一には対応していません。レジストリ キーに関するすべてのアクセス規則のセットを取得する場合、このセットには、すべてのアクセス制御エントリを表すために現在必要な最低限の数の規則だけが含まれます。

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

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

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

使用例使用例

継承と反映を行うアクセス規則のコード例を次に示します。この例では、RegistrySecurity オブジェクトを作成し、ContainerInherit フラグを持つ 2 つの規則を作成して追加します。最初の規則には、反映フラグがありません。2 番目の規則には、NoPropagateInherit と InheritOnly が存在します。

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

Option Explicit
Imports System
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
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
 */
継承階層継承階層
System.Object
   System.Security.AccessControl.AuthorizationRule
     System.Security.AccessControl.AccessRule
      System.Security.AccessControl.RegistryAccessRule
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule コンストラクタ (String, RegistryRights, AccessControlType)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Security.AccessControl
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    identity As String, _
    registryRights As RegistryRights, _
    type As AccessControlType _
)
Dim identity As String
Dim registryRights As RegistryRights
Dim type As AccessControlType

Dim instance As New RegistryAccessRule(identity,
 registryRights, type)
public RegistryAccessRule (
    string identity,
    RegistryRights registryRights,
    AccessControlType type
)
public:
RegistryAccessRule (
    String^ identity, 
    RegistryRights registryRights, 
    AccessControlType type
)
public RegistryAccessRule (
    String identity, 
    RegistryRights registryRights, 
    AccessControlType type
)
public function RegistryAccessRule (
    identity : String, 
    registryRights : RegistryRights, 
    type : AccessControlType
)

パラメータ

identity

規則の適用対象となるユーザー名またはグループ名。

registryRights

許可されたアクセス権または拒否されたアクセス権を表す RegistryRights 値のビットごとの組み合わせ。

type

アクセス権が許可されているかどうかを表す AccessControlType 値の 1 つ。

例外例外
例外の種類条件

ArgumentOutOfRangeException

registryRights に無効な値が指定されています。

または

type に無効な値が指定されています。

ArgumentNullException

registryRights が 0 です。

ArgumentException

identity が null 参照 (Visual Basic では Nothing) です。

または

identity は、長さ 0 の文字列です。

または

identity が 512 文字を超えています。

解説解説

このコンストラクタは、既定の反映と継承を指定します。つまり、InheritanceFlags.None と PropagationFlags.None です。

このコンストラクタは、identity を System.Security.Principal.NTAccount(String) コンストラクタに渡し、新しく作成した NTAccount オブジェクトを RegistryAccessRule(IdentityReference,RegistryRights,AccessControlType) コンストラクタに渡して、NTAccount オブジェクトを作成するのと同じ働きをします。

使用例使用例

レジストリ アクセス規則を作成し、RegistrySecurity オブジェクトに追加するコード例を次に示します。また、アクセス権を許可する規則と拒否する規則は別々に保存され、同じ種類で互換性がある規則はマージされることも示します。

Imports System
Imports Microsoft.Win32
Imports System.Security.AccessControl
Imports System.Security.Principal

Public Class Example

    Public Shared Sub Main()

        ' Create a string representing the current user.
        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 the key.
        Dim rule As New
 RegistryAccessRule(user, _
            RegistryRights.ReadKey, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        ' Add a rule that denies the current user the 
        ' right to change permissions on the Registry.
        rule = New RegistryAccessRule(user, _
            RegistryRights.ChangePermissions, _
            AccessControlType.Deny)
        mSec.AddAccessRule(rule)

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

        ' Add a rule that allows the current user the 
        ' right to read permissions on the Registry. This 
        ' rule is merged with the existing Allow rule.
        rule = New RegistryAccessRule(user, _
            RegistryRights.WriteKey, _
            AccessControlType.Allow)
        mSec.AddAccessRule(rule)

        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()
        Next

    End Sub
End Class 

'This code example produces output similar to following:
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Deny
'      Rights: ChangePermissions
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: ReadKey
'
'
'Current access rules:
'
'        User: TestDomain\TestUser
'        Type: Deny
'      Rights: ChangePermissions
'
'        User: TestDomain\TestUser
'        Type: Allow
'      Rights: SetValue, CreateSubKey, ReadKey
using System;
using Microsoft.Win32;
using System.Security.AccessControl;
using System.Security.Principal;

public class Example
{
    public static void Main()
    {
        // Create a string representing the current user.
        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 the key.
        RegistryAccessRule rule = new RegistryAccessRule(user,
 
            RegistryRights.ReadKey, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        // Add a rule that denies the current user the 
        // right to change permissions on the Registry.
        rule = new RegistryAccessRule(user, 
            RegistryRights.ChangePermissions, 
            AccessControlType.Deny);
        mSec.AddAccessRule(rule);

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

        // Add a rule that allows the current user the 
        // right to read permissions on the Registry. This 
        // rule is merged with the existing Allow rule.
        rule = new RegistryAccessRule(user, 
            RegistryRights.WriteKey, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        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();
        }
    }
}

/* This code example produces output similar to following:

Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions

        User: TestDomain\TestUser
        Type: Allow
      Rights: ReadKey


Current access rules:

        User: TestDomain\TestUser
        Type: Deny
      Rights: ChangePermissions

        User: TestDomain\TestUser
        Type: Allow
      Rights: SetValue, CreateSubKey, ReadKey
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryAccessRule クラス
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule コンストラクタ (String, RegistryRights, InheritanceFlags, PropagationFlags, AccessControlType)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Security.AccessControl
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    identity As String, _
    registryRights As RegistryRights, _
    inheritanceFlags As InheritanceFlags, _
    propagationFlags As PropagationFlags, _
    type As AccessControlType _
)
Dim identity As String
Dim registryRights As RegistryRights
Dim inheritanceFlags As InheritanceFlags
Dim propagationFlags As PropagationFlags
Dim type As AccessControlType

Dim instance As New RegistryAccessRule(identity,
 registryRights, inheritanceFlags, propagationFlags, type)
public RegistryAccessRule (
    string identity,
    RegistryRights registryRights,
    InheritanceFlags inheritanceFlags,
    PropagationFlags propagationFlags,
    AccessControlType type
)
public:
RegistryAccessRule (
    String^ identity, 
    RegistryRights registryRights, 
    InheritanceFlags inheritanceFlags, 
    PropagationFlags propagationFlags, 
    AccessControlType type
)
public RegistryAccessRule (
    String identity, 
    RegistryRights registryRights, 
    InheritanceFlags inheritanceFlags, 
    PropagationFlags propagationFlags, 
    AccessControlType type
)
public function RegistryAccessRule (
    identity : String, 
    registryRights : RegistryRights, 
    inheritanceFlags : InheritanceFlags, 
    propagationFlags : PropagationFlags, 
    type : AccessControlType
)

パラメータ

identity

規則の適用対象となるユーザー名またはグループ名。

registryRights

許可されたアクセス権または拒否されたアクセス権を表す RegistryRights 値のビットごとの組み合わせ。

inheritanceFlags

他のオブジェクトからアクセス権を継承する方法を指定する InheritanceFlags フラグのビットごとの組み合わせ。

propagationFlags

他のオブジェクトにアクセス権を反映させる方法を指定する PropagationFlags フラグのビットごとの組み合わせ。

type

アクセス権が許可されているかどうかを指定する AccessControlType 値の 1 つ。

例外例外
例外の種類条件

ArgumentOutOfRangeException

registryRights に無効な値が指定されています。

または

type に無効な値が指定されています。

または

inheritanceFlags に無効な値が指定されています。

または

propagationFlags に無効な値が指定されています。

ArgumentNullException

eventRights が 0 です。

ArgumentException

identity が null 参照 (Visual Basic では Nothing) です。

または

identity は、長さ 0 の文字列です。

または

identity が 512 文字を超えています。

解説解説

すべてのレジストリ キーはコンテナなので、レジストリ キーに対して有効な継承フラグは、InheritanceFlags.ContainerInherit フラグだけです。このフラグを指定しないと、反映フラグは無視され、直接のキーだけが影響を受けます。このフラグが存在する場合、次の表に示すように、規則が反映されます。この表では、サブキー S が存在し、このサブキー S に子サブキー CS と孫サブキー GS が含まれていることを前提にしています。そのため、孫サブキーへのパスは、S\CS\GS になります。

反映フラグ

S

CS

GS

None

X

X

X

NoPropagateInherit

X

X

 

InheritOnly

 

X

X

NoPropagateInherit, InheritOnly

 

X

 

孫サブキーのパターンは、孫サブキーによって格納されるすべてのサブキーに適用されます。

たとえば、inheritanceFlags に対して ContainerInherit フラグを指定し、propagationFlags に対して InheritOnly 反映フラグを指定すると、この規則は、直接のサブキーには適用されず、そのすべての直接の子サブキーとそれらの子サブキーに格納されるすべてのサブキーに適用されます。

メモメモ

inheritanceFlags に InheritanceFlags.ObjectInherit フラグを指定することもできますが、意味がありません。アクセス制御という目的上、サブキー内の名前と値のペアは別個のオブジェクトにはなっていません。名前と値のペアに対するアクセス権は、サブキーの権限によって制御されます。さらに、すべてのサブキーがコンテナであり、他のサブキーを含めることができるため、ObjectInherit フラグの影響を受けません。また、ObjectInherit フラグを指定すると、互換性がある規則に干渉するため、規則の管理が必要以上に困難になります。

このコンストラクタは、identity を System.Security.Principal.NTAccount(String) コンストラクタに渡し、新しく作成した NTAccount オブジェクトを RegistryAccessRule(IdentityReference,RegistryRights,InheritanceFlags,PropagationFlags,AccessControlType) コンストラクタに渡して、NTAccount オブジェクトを作成するのと同じ働きをします。

使用例使用例

継承と反映を行うアクセス規則のコード例を次に示します。この例では、RegistrySecurity オブジェクトを作成し、ContainerInherit フラグを持つ 2 つの規則を作成して追加します。最初の規則には、反映フラグがありません。2 番目の規則には、NoPropagateInherit と InheritOnly が存在します。

このプログラムでは、RegistrySecurity オブジェクト内の規則を表示し、RegistrySecurity オブジェクトを使用してサブキーを作成します。さらに、子サブキーと孫サブキーを作成し、各サブキーの規則を表示します。最後に、テスト キーを削除します。

Option Explicit
Imports System
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
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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryAccessRule クラス
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule コンストラクタ (IdentityReference, RegistryRights, AccessControlType)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Security.AccessControl
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    identity As IdentityReference, _
    registryRights As RegistryRights, _
    type As AccessControlType _
)
Dim identity As IdentityReference
Dim registryRights As RegistryRights
Dim type As AccessControlType

Dim instance As New RegistryAccessRule(identity,
 registryRights, type)
public RegistryAccessRule (
    IdentityReference identity,
    RegistryRights registryRights,
    AccessControlType type
)
public:
RegistryAccessRule (
    IdentityReference^ identity, 
    RegistryRights registryRights, 
    AccessControlType type
)
public RegistryAccessRule (
    IdentityReference identity, 
    RegistryRights registryRights, 
    AccessControlType type
)
public function RegistryAccessRule (
    identity : IdentityReference, 
    registryRights : RegistryRights, 
    type : AccessControlType
)

パラメータ

identity

規則の適用対象となるユーザーまたはグループ。SecurityIdentifier 型にするか、または SecurityIdentifier 型に変換できる NTAccount などの型にする必要があります。

registryRights

許可されたアクセス権または拒否されたアクセス権を表す RegistryRights 値のビットごとの組み合わせ。

type

アクセス権が許可されているかどうかを表す AccessControlType 値の 1 つ。

例外例外
例外の種類条件

ArgumentOutOfRangeException

registryRights に無効な値が指定されています。

または

type に無効な値が指定されています。

ArgumentNullException

identity が null 参照 (Visual Basic では Nothing) です。

または

eventRights が 0 です。

ArgumentException

identity が SecurityIdentifier 型ではなく、SecurityIdentifier 型に変換できる NTAccount などの型でもありません。

解説解説

このコンストラクタは、既定の反映と継承を指定します。つまり、InheritanceFlags.None と PropagationFlags.None です。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryAccessRule クラス
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule コンストラクタ (IdentityReference, RegistryRights, InheritanceFlags, PropagationFlags, AccessControlType)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Security.AccessControl
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    identity As IdentityReference, _
    registryRights As RegistryRights, _
    inheritanceFlags As InheritanceFlags, _
    propagationFlags As PropagationFlags, _
    type As AccessControlType _
)
Dim identity As IdentityReference
Dim registryRights As RegistryRights
Dim inheritanceFlags As InheritanceFlags
Dim propagationFlags As PropagationFlags
Dim type As AccessControlType

Dim instance As New RegistryAccessRule(identity,
 registryRights, inheritanceFlags, propagationFlags, type)
public RegistryAccessRule (
    IdentityReference identity,
    RegistryRights registryRights,
    InheritanceFlags inheritanceFlags,
    PropagationFlags propagationFlags,
    AccessControlType type
)
public:
RegistryAccessRule (
    IdentityReference^ identity, 
    RegistryRights registryRights, 
    InheritanceFlags inheritanceFlags, 
    PropagationFlags propagationFlags, 
    AccessControlType type
)
public RegistryAccessRule (
    IdentityReference identity, 
    RegistryRights registryRights, 
    InheritanceFlags inheritanceFlags, 
    PropagationFlags propagationFlags, 
    AccessControlType type
)
public function RegistryAccessRule (
    identity : IdentityReference, 
    registryRights : RegistryRights, 
    inheritanceFlags : InheritanceFlags, 
    propagationFlags : PropagationFlags, 
    type : AccessControlType
)

パラメータ

identity

規則の適用対象となるユーザーまたはグループ。SecurityIdentifier 型にするか、または SecurityIdentifier 型に変換できる NTAccount などの型にする必要があります。

registryRights

許可されたアクセス権または拒否されたアクセス権を指定する RegistryRights 値のビットごとの組み合わせ。

inheritanceFlags

他のオブジェクトからアクセス権を継承する方法を指定する InheritanceFlags フラグのビットごとの組み合わせ。

propagationFlags

他のオブジェクトにアクセス権を反映させる方法を指定する PropagationFlags フラグのビットごとの組み合わせ。

type

アクセス権が許可されているかどうかを指定する AccessControlType 値の 1 つ。

例外例外
例外の種類条件

ArgumentOutOfRangeException

registryRights に無効な値が指定されています。

または

type に無効な値が指定されています。

または

inheritanceFlags に無効な値が指定されています。

または

propagationFlags に無効な値が指定されています。

ArgumentNullException

identity が null 参照 (Visual Basic では Nothing) です。

または

registryRights が 0 です。

ArgumentException

identity が SecurityIdentifier 型ではなく、SecurityIdentifier 型に変換できる NTAccount などの型でもありません。

解説解説

すべてのレジストリ キーはコンテナなので、レジストリ キーに対して有効な継承フラグは、InheritanceFlags.ContainerInherit フラグだけです。このフラグを指定しないと、反映フラグは無視され、直接のキーだけが影響を受けます。このフラグが存在する場合、次の表に示すように、規則が反映されます。この表では、サブキー S が存在し、このサブキー S に子サブキー CS と孫サブキー GS が含まれていることを前提にしています。そのため、孫サブキーへのパスは、S\CS\GS になります。

反映フラグ

S

CS

GS

None

X

X

X

NoPropagateInherit

X

X

 

InheritOnly

 

X

X

NoPropagateInherit, InheritOnly

 

X

 

孫サブキーのパターンは、孫サブキーによって格納されるすべてのサブキーに適用されます。

たとえば、inheritanceFlags に対して ContainerInherit フラグを指定し、propagationFlags に対して InheritOnly 反映フラグを指定すると、この規則は、直接のサブキーには適用されず、そのすべての直接の子サブキーとそれらの子サブキーに格納されるすべてのサブキーに適用されます。

メモメモ

inheritanceFlags に InheritanceFlags.ObjectInherit フラグを指定することもできますが、意味がありません。アクセス制御という目的上、サブキー内の名前と値のペアは別個のオブジェクトにはなっていません。名前と値のペアに対するアクセス権は、サブキーの権限によって制御されます。さらに、すべてのサブキーがコンテナであり、他のサブキーを含めることができるため、ObjectInherit フラグの影響を受けません。また、ObjectInherit フラグを指定すると、互換性がある規則に干渉するため、規則の管理が必要以上に困難になります。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RegistryAccessRule クラス
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule コンストラクタ

RegistryAccessRule クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

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

関連項目

RegistryAccessRule クラス
RegistryAccessRule メンバ
System.Security.AccessControl 名前空間

RegistryAccessRule プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ AccessControlType  この AccessRule オブジェクトに関連付けられている AccessControlType 値を取得します。 ( AccessRule から継承されます。)
パブリック プロパティ IdentityReference  この規則を適用する IdentityReference を取得します。 ( AuthorizationRule から継承されます。)
パブリック プロパティ InheritanceFlags  この規則を子オブジェクトが継承する方法を決定するフラグの値を取得します。 ( AuthorizationRule から継承されます。)
パブリック プロパティ IsInherited  この規則を明示的に設定するか、または親コンテナ オブジェクトから継承するかを指定する値を取得します。 ( AuthorizationRule から継承されます。)
パブリック プロパティ PropagationFlags  反映フラグの値を取得します。このフラグから、この規則を子オブジェクトに反映させる方法を判断します。このプロパティが重要なのは、InheritanceFlags 列挙体の値が None でない場合だけです。 ( AuthorizationRule から継承されます。)
パブリック プロパティ RegistryRights アクセス規則で許可されたアクセス権または拒否されたアクセス権を取得します。
参照参照

関連項目

RegistryAccessRule クラス
System.Security.AccessControl 名前空間

RegistryAccessRule メソッド


RegistryAccessRule メンバ

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

RegistryAccessRule データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド RegistryAccessRule オーバーロードされます。 RegistryAccessRule クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ AccessControlType  この AccessRule オブジェクトに関連付けられている AccessControlType 値を取得します。(AccessRule から継承されます。)
パブリック プロパティ IdentityReference  この規則を適用する IdentityReference を取得します。(AuthorizationRule から継承されます。)
パブリック プロパティ InheritanceFlags  この規則を子オブジェクトが継承する方法を決定するフラグの値を取得します。(AuthorizationRule から継承されます。)
パブリック プロパティ IsInherited  この規則を明示的に設定するか、または親コンテナ オブジェクトから継承するかを指定する値を取得します。(AuthorizationRule から継承されます。)
パブリック プロパティ PropagationFlags  反映フラグの値を取得します。このフラグから、この規則を子オブジェクトに反映させる方法を判断します。このプロパティが重要なのは、InheritanceFlags 列挙体の値が None でない場合だけです。(AuthorizationRule から継承されます。)
パブリック プロパティ RegistryRights アクセス規則で許可されたアクセス権または拒否されたアクセス権を取得します。
パブリック メソッドパブリック メソッド
参照参照

関連項目

RegistryAccessRule クラス
System.Security.AccessControl 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「RegistryAccessRule」の関連用語

RegistryAccessRuleのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RegistryAccessRuleのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS