RegistryAccessPermission Class

Namespace: System.Net.Security

Assembly: System.dll

Description

Represents the permission to access the Windows registry. This class cannot be inherited.

Remarks

The RegistryAccessPermission class provides a way to control access to registry keys. It is part of the Code Access Security (CAS) model in .NET Framework. Applications can use this permission to grant or deny specific access rights to registry locations for code running within a particular security context.

This permission is typically used in conjunction with other security attributes or policies to enforce fine-grained access control to sensitive registry data.

Note: Code Access Security (CAS) has been deprecated and is not supported in .NET 5 and later versions. This information is provided for historical context and for working with older .NET Framework applications.

Members

Constructors

Name Description
RegistryAccessPermission(PermissionState state) Initializes a new instance of the RegistryAccessPermission class with the specified permission state.

Methods

Name Description
Copy() Creates and returns an identical copy of the current permission object.
Equals(object obj) Determines whether the specified object is equal to the current object.
FromXml(SecurityElement e) Reconstructs a security object from an XML encoding.
GetHashCode() Serves as a hash function for the current type.
Intersect(IPermission target) When overridden in a derived class, creates and returns a new permission that represents the intersection of the current permission and the specified permission.
IsSubsetOf(IPermission target) When overridden in a derived class, determines whether the current permission is a subset of the specified permission.
IsUnrestricted() When overridden in a derived class, indicates whether the permission can be exercised without having to examine the permission state.
ToXml() Returns an XML encoding of the security object.
Union(IPermission target) When overridden in a derived class, creates and returns a new permission that represents the set of all points that are contained in both the current permission and the specified permission.

Properties

Name Description
IsUnrestricted Gets a value indicating whether the current permission is unrestricted.

Inherited Members

This class inherits members from the following base classes:

Example

The following C# code demonstrates how to grant RegistryAccessPermission to a specific code group:


using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;

public class RegistryAccessExample
{
    public static void GrantRegistryAccess()
    {
        // Define the code group that needs registry access
        CodeGroup codeGroup = new FileCodeGroup(
            new StrongNameMembershipCondition(new StrongNamePublicKeyBlob(new byte[] { /* your public key bytes */ })),
            FileIOPermissionAccess.Read | FileIOPermissionAccess.Write
        );

        // Create a RegistryAccessPermission
        RegistryAccessPermission registryPermission = new RegistryAccessPermission(PermissionState.Unrestricted);

        // Create a NamedPermissionSet to hold the permissions
        NamedPermissionSet permissionSet = new NamedPermissionSet("MyRegistryAccessSet");
        permissionSet.AddPermission(registryPermission);

        // Add the permission set to the code group's policy statement
        codeGroup.PolicyStatement.PermissionSet = permissionSet;

        Console.WriteLine("RegistryAccessPermission granted to the specified code group.");
    }

    public static void Main(string[] args)
    {
        // Note: This is a conceptual example. Actual policy manipulation requires
        // administrative privileges and is typically done via code access security tools.
        // In modern .NET, this CAS model is deprecated.

        // GrantRegistryAccess();
        Console.WriteLine("Conceptual example of granting RegistryAccessPermission.");
    }
}