RegistrySecurity.AddAccessRule(RegistryAccessRule) Yöntem

Tanım

Yeni kuralın birleştirilebileceği eşleşen bir erişim denetimi arar. Hiçbir kural bulunmazsa yeni kuralı ekler.

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)

Parametreler

rule
RegistryAccessRule

Eklenecek erişim denetimi kuralı.

Özel durumlar

rule, null'e eşittir.

Örnekler

Aşağıdaki kod örneği kayıt defteri erişim kuralları oluşturur ve bunları bir nesneye ekler ve aynı türdeki uyumlu kurallar birleştirilirken RegistrySecurity haklara izin veren ve reddeden kuralların nasıl ayrı kaldığını gösterir.

Note

Bu örnek, güvenlik nesnesini bir RegistryKey nesneye eklemez. Güvenlik nesneleri ekleyen örnekler ve RegistryKey.GetAccessControliçinde RegistryKey.SetAccessControl bulunabilir.

Devralma ve yayma bayraklarını gösteren bir kod örneği sınıfında RegistryAccessRule bulunabilir.

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

Açıklamalar

yöntemi, AddAccessRule ile aynı kullanıcı veya grup ile aynı AccessControlTyperuleolan kuralları arar. Hiçbiri bulunmazsa eklenir rule . Eşleşen bir kural bulunursa, içindeki rule haklar mevcut kuralla birleştirilir.

Kurallar farklı devralma bayrakları varsa birleştirilemez. Örneğin, bir kullanıcıya devralma bayrağı olmadan okuma erişimine izin verilirse ve AddAccessRule alt anahtarlarInheritanceFlags.ContainerInherit için devralma ile kullanıcıya yazma erişimi veren bir kural eklemek için kullanılırsa, iki kural birleştirilemez.

Farklı AccessControlType değerlere sahip kurallar hiçbir zaman birleştiremez.

Kurallar hakları en ekonomik şekilde ifade ediyor. Örneğin, bir kullanıcının QueryValuesNotify hakları varsa ve ReadPermissions haklara izin veren EnumerateSubKeys bir kural eklerseniz, kullanıcının tüm kurucu hakları vardırReadKey. Kullanıcının haklarını sorgularsanız hakları içeren ReadKey bir kural görürsünüz. Benzer şekilde, hakları kaldırırsanız EnumerateSubKeys , diğer hak bileşenleri ReadKey yeniden görünür.

Şunlara uygulanır