RegistrySecurity.RemoveAccessRule(RegistryAccessRule) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vyhledá pravidlo řízení přístupu se stejným uživatelem a AccessControlType (povolí nebo odepře) jako zadané pravidlo přístupu a s kompatibilními příznaky dědičnosti a šíření. Pokud se takové pravidlo najde, odeberou se z něj práva obsažená v zadaném pravidle přístupu.
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
Parametry
- rule
- RegistryAccessRule
A RegistryAccessRule , který určuje uživatele a AccessControlType má hledat, a sadu dědičnosti a šíření příznaků, které musí být kompatibilní s odpovídajícím pravidlem, pokud je nalezeno. Určuje práva k odebrání z kompatibilního pravidla, pokud je nalezeno.
Návraty
true je-li nalezeno kompatibilní pravidlo; jinak false.
Výjimky
rule je null.
Příklady
Následující příklad kódu ukazuje, jak RemoveAccessRule metoda odebere práva z kompatibilního pravidla a jak AddAccessRule metoda slučuje práva s kompatibilními pravidly.
Příklad vytvoří RegistrySecurity objekt a přidá pravidlo, které umožňuje aktuální uživatelská RegistryRights.ReadKey práva. Příklad pak vytvoří pravidlo, které udělí uživateli RegistryRights.SetValue, se stejnou dědičností a šíření práv jako první pravidlo, a použije metodu RemoveAccessRule k odebrání tohoto nového pravidla z objektu RegistrySecurity . SetValue je součástí ReadKey, takže je odebrán z kompatibilního pravidla. Zobrazí se pravidla v objektu RegistrySecurity se zbývajícími složkami ReadKey.
Ukázkový kód pak volá metodu RemoveAccessRule , která sloučí SetValue právo zpět do pravidla v objektu RegistrySecurity .
Note
Tento příklad nepřipokládá objekt zabezpečení k objektu RegistryKey . Druhý příklad v této části připojí objekt zabezpečení, a proto proveďte příklady v 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
'
Poznámky
RegistrySecurity Aktuální vyhledá pravidlo, které má stejného uživatele a stejnou AccessControlType hodnotu jako rule. Pokud se takové pravidlo nenajde, neprovedou se žádné akce a metoda vrátí false. Pokud jsou nalezena odpovídající pravidla, jejich dědičnost a příznaky kompatibility jsou kontrolovány z důvodu kompatibility s příznaky uvedenými v rule. Pokud není nalezeno žádné kompatibilní pravidlo, není provedena žádná akce a metoda vrátí false. Pokud se najde pravidlo s kompatibilními příznaky, práva zadaná v rule kompatibilním pravidle se odeberou a metoda vrátí true. Pokud rule specifikuje práva, která nejsou obsažena v kompatibilním pravidle, není přijata žádná akce s ohledem na tato práva. Pokud jsou všechna práva odebrána z kompatibilního pravidla, odebere se z aktuálního RegistrySecurity objektu celé pravidlo.