共用方式為


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

要加入的存取控制規則。

例外狀況

rulenull

範例

下列程式碼範例會建立登錄存取規則,並將其新增至 RegistrySecurity 物件,其中顯示允許和拒絕許可權的規則如何保持分開,同時合併相同類型的相容規則。

注意

這個範例不會將安全性物件附加至 RegistryKey 物件。 您可以在 和 RegistryKey.SetAccessControl 中找到 RegistryKey.GetAccessControl 附加安全性物件的範例。

您可以在 類別中找到示範繼承和傳播旗標的程式 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 會搜尋具有相同使用者或群組的規則,且與 rule 相同 AccessControlType 。 如果找不到, rule 則會新增 。 如果找到相符的規則,中的 rule 許可權會與現有的規則合併。

如果規則有不同的繼承旗標,則無法合併。 例如,如果使用者被允許讀取權限且沒有繼承旗標,並且 AddAccessRule 用來新增規則,讓使用者寫入存取子機碼 (InheritanceFlags.ContainerInherit) ,則無法合併這兩個規則。

具有不同 AccessControlType 值的規則永遠不會合並。

規則以最經濟的方式表達權利。 例如,如果使用者具有 、 和 許可權,而且您新增了允許 EnumerateSubKeys 許可權的規則,則使用者具有許可權的所有組成部分 ReadKeyReadPermissionsNotifyQueryValues 如果您查詢使用者的許可權,您會看到包含 ReadKey 許可權的規則。 同樣地,如果您移除 EnumerateSubKeys 許可權,其他權利組成 ReadKey 將會重新出現。

適用於