Language

RegistrySecurity.RemoveAccessRule(RegistryAccessRule) メソッド

定義

指定したアクセス規則と同じユーザーと AccessControlType (許可または拒否) を持ち、互換性のある継承と伝達フラグを持つアクセス制御規則を検索します。そのような規則が見つかった場合は、指定したアクセス規則に含まれる権限が削除されます。

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

パラメーター

rule
RegistryAccessRule

検索するユーザーとRegistryAccessRuleを指定するAccessControlTypeと、一致するルールが見つかった場合は互換性が必要な継承フラグと伝達フラグのセット。 互換性のあるルールが見つかった場合に削除する権限を指定します。

返品

true 互換性のあるルールが見つかった場合。それ以外の場合は false。

例外

rule は nullです。

例

次のコード例は、 RemoveAccessRule メソッドが互換性のある規則から権限を削除する方法と、 AddAccessRule メソッドが互換性のある規則と権限をマージする方法を示しています。

この例では、 RegistrySecurity オブジェクトを作成し、現在のユーザー RegistryRights.ReadKey 権限を許可する規則を追加します。 次に、最初のルールと同じ継承と伝達権限を持つユーザー RegistryRights.SetValueを付与するルールを作成し、 RemoveAccessRule メソッドを使用して、 RegistrySecurity オブジェクトからこの新しいルールを削除します。 SetValue は ReadKeyの構成要素であるため、互換性のある規則から削除されます。 RegistrySecurity オブジェクトのルールが表示され、ReadKeyの残りの構成要素が表示されます。

次に、RemoveAccessRule メソッドを呼び出して、SetValue オブジェクト内のルールにRegistrySecurityをマージします。

Note

この例では、セキュリティ オブジェクトを RegistryKey オブジェクトにアタッチしません。 このセクションの 2 番目の例では、セキュリティ オブジェクトをアタッチします。また、 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
'

注釈

現在のRegistrySecurityは、AccessControlTypeと同じユーザーと同じrule値を持つルールを検索します。 このようなルールが見つからない場合、アクションは実行されません。メソッドは falseを返します。 一致する規則が見つかった場合、継承フラグと互換性フラグは、 ruleで指定されたフラグとの互換性をチェックします。 互換性のあるルールが見つからない場合、アクションは実行されません。メソッドは falseを返します。 互換性のあるフラグを持つ規則が見つかった場合、 rule で指定された権限は互換性のある規則から削除され、メソッドは trueを返します。 ruleが互換性のある規則に含まれていない権限を指定した場合、それらの権限に対するアクションは実行されません。 互換性のあるルールからすべての権限が削除された場合、ルール全体が現在の RegistrySecurity オブジェクトから削除されます。

適用対象