This documentation is archived and is not being maintained.

NamedPermissionSet Class

Defines a permission set that has a name and description associated with it. This class cannot be inherited.

Namespace: System.Security
Assembly: mscorlib (in mscorlib.dll)

public sealed class NamedPermissionSet : PermissionSet
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class NamedPermissionSet extends PermissionSet
public final class NamedPermissionSet extends PermissionSet
Not applicable.

Named permission sets are used in security policy administration to specify the permissions to be granted to code that belongs to certain code groups. Names are strings of alphanumeric characters. Description strings can consist of any printable characters. For more information, see Named Permission Sets.

The following code example shows the use of members of the NamedPermissionSet class.

using System;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.IO;
using System.Collections;

class NamedPermissionSetDemo
    public static void PermissionSetDemo()
        Console.WriteLine("Executing NamedPermissionSetDemo");
          // Create a new named permission set and add it to Machine policy.
            NamedPermissionSet namedPS1 = CreateCompanyPermission();
            Console.WriteLine("The name of the custom named permission set is " + namedPS1.Name + "\n");
            Console.WriteLine("The description of the custom named permission set is " + namedPS1.Description + "\n");
            NamedPermissionSet namedPS2 = new NamedPermissionSet("MyPermssionSetCopy");
            // Perform a ToXml/FromXml round trip.
            Console.WriteLine("\nResult of the ToXml/FromXml round trip:");

            // For simplicity the results are displayed using a method call.

            // Create and display a copy of a permission set.
            NamedPermissionSet namedPS3 = (NamedPermissionSet)namedPS2.Copy();
            Console.WriteLine("Is the copy equal to the original? " + namedPS2.Equals(namedPS3));
            NamedPermissionSet namedPS4 = new NamedPermissionSet("Second copy", namedPS3);
            Console.WriteLine("The name of the new permission set is " + namedPS4.Name + "\n");
            // Show that the new named permission set has the same permissions as the original.
            // The hash code for two instances of the same permission might be different, hence a hash code should not be used to 
            // compare two named permission sets.
            Console.WriteLine("The hash code of the original permission set is " + namedPS2.GetHashCode());
            Console.WriteLine("The hash code of the copy is " + namedPS4.GetHashCode());

        catch (Exception e)
            Console.WriteLine("Exception thrown: " + e.Message.ToString());

    public static bool DisplayPermissions(NamedPermissionSet namedPS1)
        // Display results of namedPS.GetEnumerator.
        IEnumerator psEnumerator = namedPS1.GetEnumerator();

        while (psEnumerator.MoveNext())

        return true;
    // The following method uses the LocalIntranet permission set to create
    // a custom permission set named MyCompany.  The new permission set is
    // added to local Machine policy.  The custom named permission set is returned.
    private static NamedPermissionSet CreateCompanyPermission()
        IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();

        // Move through the policy levels to the Machine policy level.
        while (policyEnumerator.MoveNext())
            PolicyLevel currentLevel = (PolicyLevel)policyEnumerator.Current;

            if (currentLevel.Label == "Machine")
                // Enumerate the permission sets in the Machine policy level.
                IList namedPermissions = currentLevel.NamedPermissionSets;
                IEnumerator namedPermission = namedPermissions.GetEnumerator();

                // Locate the LocalIntranet permission set.
                while (namedPermission.MoveNext())
                    if (((NamedPermissionSet)namedPermission.Current).Name == "LocalIntranet")
                        // The current permission set is a copy of the LocalIntranet permission set.
                        // It can be modified to provide the permissions for the new permission set.
                        // Rename the copy to the name chosen for the new permission set.
                        ((NamedPermissionSet)namedPermission.Current).Name = "MyCompany";
                        ((NamedPermissionSet)namedPermission.Current).Description = "My custom named permission set";
                        IEnumerator permissions = ((NamedPermissionSet)namedPermission.Current).GetEnumerator();

                        // Remove the current security permission from the permission set and replace it 
                        // with a new security permission that does not have the right to assert permissions.
                        while (permissions.MoveNext())
                            if (permissions.Current.GetType().ToString() == "System.Security.Permissions.SecurityPermission")
                                // Remove the current security permission.

                                // Add a new security permission that only allows execution.
                                ((NamedPermissionSet)namedPermission.Current).AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

                            // If you run this application twice, the following instruction throws
                            // an exception because the named permission set already exists.
                            // You can remove the custom named permission set using either Caspole.exe or the  
                            // .NET Framework Configuration tool (Mscorcfg.msc).
                            return (NamedPermissionSet)namedPermission.Current;
                        // Catch the exception for a duplicate permission set.
                        catch (System.ArgumentException e)
                            Console.WriteLine(e.Message + "\n");
                            return (NamedPermissionSet)namedPermission.Current;
        // The following code is executed only if the LocalIntranet permission set has been removed.
        return new NamedPermissionSet("Nothing");

    // Test harness.
    static void Main(string[] args)
        Console.WriteLine("Press any key to exit.");


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0