Share via


RegistrySecurity.AddAccessRule(RegistryAccessRule) 메서드

정의

새 규칙을 병합할 수 있는 일치하는 액세스 제어를 검색합니다. 아무 것도 발견되지 않으면 새 규칙을 추가합니다.

public:
 void AddAccessRule(System::Security::AccessControl::RegistryAccessRule ^ rule);
public void AddAccessRule (System.Security.AccessControl.RegistryAccessRule rule);
override this.AddAccessRule : System.Security.AccessControl.RegistryAccessRule -> unit
Public Sub AddAccessRule (rule As RegistryAccessRule)

매개 변수

rule
RegistryAccessRule

추가할 액세스 제어 규칙입니다.

예외

rule이(가) null인 경우

예제

다음 코드 예제에서는 레지스트리 액세스 규칙을 만들고 개체에 RegistrySecurity 추가하여 권한을 허용 및 거부하는 규칙이 분리된 상태로 유지되는 반면, 동일한 종류의 호환되는 규칙이 병합되는 방법을 보여 주며,

참고

이 예제에서는 보안 개체를 개체에 RegistryKey 연결하지 않습니다. 보안 개체를 연결하는 예제는 다음에서 RegistryKey.GetAccessControl RegistryKey.SetAccessControl찾을 수 있습니다.

상속 및 전파 플래그를 보여 주는 코드 예제는 클래스에서 RegistryAccessRule 찾을 수 있습니다.

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
 */
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

설명

메서드는 AddAccessRule 동일한 사용자 또는 그룹과 동일한 AccessControlType rule규칙을 검색합니다. 찾을 rule 수 없는 경우 추가됩니다. 일치하는 규칙을 찾은 경우 해당 권한 rule 은 기존 규칙과 병합됩니다.

다른 상속 플래그가 있는 경우 규칙을 병합할 수 없습니다. 예를 들어 사용자가 상속 플래그 없이 읽기 액세스를 허용하고 AddAccessRule 하위 키()InheritanceFlags.ContainerInherit에 대한 상속을 사용하여 사용자에게 쓰기 권한을 부여하는 규칙을 추가하는 데 사용되는 경우 두 규칙을 병합할 수 없습니다.

값이 다른 AccessControlType 규칙은 병합되지 않습니다.

규칙은 가장 경제적인 방법으로 권리를 표현합니다. 예를 들어 사용자에게 QueryValuesNotify 권한이 있고 ReadPermissions 권한을 허용하는 EnumerateSubKeys 규칙을 추가하는 경우 사용자에게는 모든 구성 요소의 ReadKey 권한이 있습니다. 사용자의 권한을 쿼리하는 경우 권한이 포함된 ReadKey 규칙이 표시됩니다. 마찬가지로, 권리를 제거 EnumerateSubKeys 하면 다른 권리 구성 요소가 ReadKey 다시 나타납니다.

적용 대상