Export (0) Print
Expand All

SecurityPermission Class

Describes a set of security permissions applied to code. This class cannot be inherited.

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class SecurityPermission : CodeAccessPermission, 
	IUnrestrictedPermission

This permission uses the SecurityPermissionFlag enumeration. The values for this enumeration can be found in its documentation.

The following code example demonstrates the behavior of the SecurityPermission methods.

The example is intended to show how the methods perform if you execute the methods from your code. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications.

For an example showing the use of the SecurityPermission class to demand permission for a specific SecurityPermissionFlag enumeration value, see the SecurityPermission(SecurityPermissionFlag) constructor.

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

[assembly:CLSCompliant(true)]
public class SecurityPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the specified permission. 
    private bool IsSubsetOfDemo()
    {
        bool returnValue = true;
        SecurityPermissionFlag security1,security2;
        SecurityPermission securityPerm1,securityPerm2;
        SecurityGenerator SecurityGen1 = new SecurityGenerator();
        SecurityGenerator SecurityGen2 = new SecurityGenerator();
        SecurityGen1.ResetIndex();
        while(SecurityGen1.GetSecurityFlag( out security1 ))
        {
            securityPerm1 = new SecurityPermission(security1);
            SecurityGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");

            while(SecurityGen2.GetSecurityFlag( out security2 ))
            {
                securityPerm2 = new SecurityPermission(security2);
                try
                {
                    if (securityPerm1.IsSubsetOf(securityPerm2))
                    {
                        Console.WriteLine(securityPerm1.Flags + " is a subset of " +
                            securityPerm2.Flags);
                    }
                    else
                    {
                        Console.WriteLine(securityPerm1.Flags + " is not a subset of " +
                            securityPerm2.Flags);
                    }
                }
                catch(Exception e)
                {
                    Console.WriteLine("An exception was thrown for subset :" + securityPerm1 + "\n" +
                        securityPerm2 +"\n" + e);
                    returnValue=false;
                }
            }
        }
        return returnValue;
    }
    // Union creates a new permission that is the union of the current permission and the specified permission. 
    private bool UnionDemo()
    {
        bool returnValue = true;
        SecurityPermissionFlag security1,security2;
        SecurityPermission securityPerm1,securityPerm2;
        IPermission SecurityPerm3;
        SecurityGenerator SecurityGen1 = new SecurityGenerator();
        SecurityGenerator SecurityGen2 = new SecurityGenerator();
        SecurityGen1.ResetIndex();
        while(SecurityGen1.GetSecurityFlag(  out security1 ))
        {
            securityPerm1 = new SecurityPermission(security1);
            Console.WriteLine("**********************************************************\n");
            SecurityGen2.ResetIndex();

            while(SecurityGen2.GetSecurityFlag( out security2 ))
            {
                securityPerm2 = new SecurityPermission(security2);
                SecurityPerm3 = (SecurityPermission)securityPerm1.Union(securityPerm2);
                SecurityPerm3 = securityPerm1.Union(securityPerm2);

                if(SecurityPerm3 == null)
                {
                    Console.WriteLine("The union of " + security1 + " and " + security2 + " is null." );
                }
                else
                {
                    Console.WriteLine("The union of " + securityPerm1.Flags + " and " +
                        securityPerm2.Flags + " = " +
                        ((SecurityPermission)SecurityPerm3).Flags.ToString());
                }

            }
        }

        return returnValue;

    }
    // Intersect creates and returns a new permission that is the intersection of the current 
    // permission and the permission specified. 
    private bool IntersectDemo()
    {
        bool returnValue = true;
        SecurityPermissionFlag security1,security2;
        SecurityPermission securityPerm1,securityPerm2,SecurityPerm3;
        SecurityGenerator SecurityGen1 = new SecurityGenerator();
        SecurityGenerator SecurityGen2 = new SecurityGenerator();
        SecurityGen1.ResetIndex();
        while(SecurityGen1.GetSecurityFlag( out security1 ))
        {
            securityPerm1 = new SecurityPermission(security1);
            Console.WriteLine("**********************************************************\n");
            SecurityGen2.ResetIndex();

            while(SecurityGen2.GetSecurityFlag( out security2 ))
            {
                securityPerm2 = new SecurityPermission(security2);

                SecurityPerm3 = (SecurityPermission)securityPerm1.Intersect(securityPerm2);
                if (SecurityPerm3 != null)
                {
                    Console.WriteLine("The intersection of " + security1 + " and " + security2 + " = " +
                        ((SecurityPermission)SecurityPerm3).Flags.ToString());
                }
                else
                {
                    Console.WriteLine("The intersection of " + security1 + " and " + security2 + " is null." );
                }
            }
        }

        return returnValue;
    }
    //Copy creates and returns an identical copy of the current permission. 
    private bool CopyDemo()
    {

        bool returnValue = true;

        SecurityPermissionFlag security1;
        SecurityPermission securityPerm1,securityPerm2;

        SecurityGenerator SecurityGen1 = new SecurityGenerator();
        SecurityGenerator SecurityGen2 = new SecurityGenerator();

        SecurityGen1.ResetIndex();
        while(SecurityGen1.GetSecurityFlag( out security1 ))
        {
            securityPerm1 = new SecurityPermission(security1);
            SecurityGen2.ResetIndex();
            Console.WriteLine("********************************************************\n");
            try
            {
                securityPerm2 = (SecurityPermission)securityPerm1.Copy();
                Console.WriteLine("Result of copy = " + securityPerm2.ToString());

            }
            catch(Exception e)
            {
                Console.WriteLine("Copy failed :" + securityPerm1.ToString() + e);
                continue;
            }
        }
        return returnValue;
    }
    // ToXml creates an XML encoding of the permission and its current state; 
    // FromXml reconstructs a permission with the specified state from the XML encoding. 
    private bool ToFromXmlDemo()
    {

        bool returnValue = true;

        SecurityPermissionFlag security1;
        SecurityPermission securityPerm1,securityPerm2;
        SecurityGenerator SecurityGen1 = new SecurityGenerator();
        SecurityGenerator SecurityGen2 = new SecurityGenerator();
        SecurityGen1.ResetIndex();
        while(SecurityGen1.GetSecurityFlag( out security1 ))
        {
            securityPerm1 = new SecurityPermission(security1);
            Console.WriteLine("********************************************************\n");
            SecurityGen2.ResetIndex();
            try
            {
                securityPerm2= new SecurityPermission(PermissionState.None);
                securityPerm2.FromXml(securityPerm1.ToXml());
                Console.WriteLine("Result of ToFromXml = " +
                    securityPerm2.ToString());

            }
            catch(Exception e)
            {
                Console.WriteLine("ToFromXml failed :" + securityPerm1.ToString() + e);
                continue;
            }
        }

        return returnValue;

    }
    // Invoke all demos. 
    public bool runDemo()
    {
        bool returnCode = true;
        bool tempReturnCode;

        // Call the IsSubsetOf demo.
        tempReturnCode= IsSubsetOfDemo();
        if(tempReturnCode)Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
        else Console.Out.WriteLine("IsSubsetOf demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the Union demo.
        tempReturnCode= UnionDemo();
        if(tempReturnCode)Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the Intersect demo.
        tempReturnCode= IntersectDemo();
        if(tempReturnCode)Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        returnCode=tempReturnCode && returnCode;


        // Call the Copy demo.
        tempReturnCode= CopyDemo();
        if(tempReturnCode)Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        returnCode=tempReturnCode && returnCode;

        // Call the ToFromXml demo.
        tempReturnCode= ToFromXmlDemo();
        if(tempReturnCode)Console.Out.WriteLine("ToFromXml demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        returnCode=tempReturnCode && returnCode;

        return ( returnCode );
    }
    // Test harness. 
    public static void Main(String[] args)
    {
        try
        {
            SecurityPermissionDemo democase = new SecurityPermissionDemo();
            bool returnCode = democase.runDemo();
            if (returnCode)
            {
                Console.Out.WriteLine("SecurityPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("SecurityPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch(Exception e)
        {
            Console.Out.WriteLine("SecurityPermission demo failed.");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}


// This class generates SecurityPermission objects. 

internal class SecurityGenerator
{


    private SecurityPermissionFlag[] mySecurity =
{
    SecurityPermissionFlag.AllFlags,
    SecurityPermissionFlag.Assertion |
    SecurityPermissionFlag.ControlAppDomain,
    SecurityPermissionFlag.ControlAppDomain,
    SecurityPermissionFlag.ControlDomainPolicy |
    SecurityPermissionFlag.ControlEvidence,
    SecurityPermissionFlag.ControlEvidence,
    SecurityPermissionFlag.ControlPolicy |
    SecurityPermissionFlag.ControlPrincipal,
    SecurityPermissionFlag.ControlPrincipal,
    SecurityPermissionFlag.ControlThread,
    SecurityPermissionFlag.Execution,
    SecurityPermissionFlag.Infrastructure,
    SecurityPermissionFlag.NoFlags,
    SecurityPermissionFlag.RemotingConfiguration |
    SecurityPermissionFlag.SerializationFormatter,
    SecurityPermissionFlag.SerializationFormatter,
    SecurityPermissionFlag.SkipVerification,
    SecurityPermissionFlag.UnmanagedCode};

    private int reflectionIndex = 0;

    public SecurityGenerator()
    {
        ResetIndex();
    }

    public void ResetIndex()
    {
        reflectionIndex = 0;
    }
    // GetSecurityFlag returns a SecurityPermissionFlag. 
    public bool GetSecurityFlag(out SecurityPermissionFlag security)
    {

        if(reflectionIndex >= mySecurity.Length)
        {
            security = SecurityPermissionFlag.NoFlags;
            reflectionIndex++;
            return false;
        }
        security = mySecurity[reflectionIndex++];
            return true;

    }
} //

System.Object
  System.Security.CodeAccessPermission
    System.Security.Permissions.SecurityPermission

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft