RegistrySecurity.RemoveAccessRule(RegistryAccessRule) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Söker efter en åtkomstkontrollregel med samma användare och AccessControlType (tillåt eller neka) som den angivna åtkomstregeln och med kompatibla arvs- och spridningsflaggor. Om en sådan regel hittas tas rättigheterna i den angivna åtkomstregeln bort från den.
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
Parametrar
- rule
- RegistryAccessRule
En RegistryAccessRule som anger användaren och AccessControlType att söka efter och en uppsättning flaggor för arv och spridning som en matchande regel, om den hittas, måste vara kompatibel med. Anger behörigheten att ta bort från den kompatibla regeln om den hittas.
Returer
true om en kompatibel regel hittas; annars false.
Undantag
rule är null.
Exempel
I följande kodexempel visas hur RemoveAccessRule metoden tar bort rättigheter från en kompatibel regel och hur AddAccessRule metoden sammanfogar rättigheter med kompatibla regler.
Exemplet skapar ett RegistrySecurity objekt och lägger till en regel som tillåter aktuella användarrättigheter RegistryRights.ReadKey . Exemplet skapar sedan en regel som ger användaren RegistryRights.SetValue, med samma arvs- och spridningsrättigheter som den första regeln, och använder metoden för att ta bort den RemoveAccessRule nya regeln från RegistrySecurity objektet. SetValue är en komponent i ReadKey, så den tas bort från den kompatibla regeln. Reglerna i objektet visas, som visar de återstående komponenterna RegistrySecurityi ReadKey .
Exempelkoden anropar RemoveAccessRule sedan metoden för att sammanfoga SetValue höger tillbaka till regeln i RegistrySecurity objektet.
Note
Det här exemplet bifogar inte säkerhetsobjektet till ett RegistryKey objekt. Det andra exemplet i det här avsnittet bifogar ett säkerhetsobjekt, och det gör även exemplen i 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
'
Kommentarer
Den aktuella RegistrySecurity söks efter en regel som har samma användare och samma AccessControlType värde som rule. Om ingen sådan regel hittas vidtas ingen åtgärd och metoden returnerar false. Om matchande regler hittas kontrolleras deras arvs- och kompatibilitetsflaggor för kompatibilitet med flaggorna som anges i rule. Om ingen kompatibel regel hittas vidtas ingen åtgärd och metoden returnerar false. Om en regel med kompatibla flaggor hittas tas de rättigheter som anges i rule bort från den kompatibla regeln och metoden returnerar true. Om rule anger rättigheter som inte ingår i den kompatibla regeln vidtas inga åtgärder med avseende på dessa rättigheter. Om alla rättigheter tas bort från den kompatibla regeln tas hela regeln bort från det aktuella RegistrySecurity objektet.