Export (0) Print
Expand All

SecurityException Class

The exception that is thrown when a security error is detected.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class SecurityException : SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SecurityException extends SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public class SecurityException extends SystemException

SecurityException uses the HRESULT COR_E_SECURITY, which has the value 0x8013150A.

For a list of the initial property values for an instance of the SecurityException class, see a specific SecurityException constructor.

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

using System;
using System.Data;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Reflection;
using System.Runtime.Serialization;

[assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse, 
    Flags = KeyContainerPermissionFlags.Import)]
namespace TestForm
{
    class EntryPoint
    {
        [STAThread]
        static void Main()
        {
            EntryPoint eP = new EntryPoint();
            eP.RunCode();
            Console.WriteLine("Press the ENTER key to exit.");
            Console.Read();
        }
        void RunCode()
        {
            try
            {
                // Deny a permission.
                KeyContainerPermission kCP1 = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP1.Deny();

                // Demand the denied permission and display the 
                // exception properties.
                Display("Demanding a denied permission. \n\n");
                DemandDeniedPermission();
                Display("************************************************\n");
                CodeAccessPermission.RevertDeny();

                // Demand the permission refused in the 
                // assembly-level attribute.
                Display("Demanding a refused permission. \n\n");
                DemandRefusedPermission();
                Display("************************************************\n");

                // Demand the permission implicitly refused through a 
                // PermitOnly attribute. Permit only the permission that 
                // will cause the failure and the security permissions 
                // necessary to display the results of the failure.
                PermissionSet permitOnly = new PermissionSet(
                    PermissionState.None);
                permitOnly.AddPermission(new KeyContainerPermission(
                    KeyContainerPermissionFlags.Import));
                permitOnly.AddPermission(new SecurityPermission(
                    SecurityPermissionFlag.ControlEvidence |
                    SecurityPermissionFlag.ControlPolicy | 
                    SecurityPermissionFlag.SerializationFormatter));
                permitOnly.PermitOnly();
                Display("Demanding an implicitly refused permission. \n\n");
                DemandPermitOnly();
            }
            catch (Exception sE)
            {
                Display("************************************************\n");
                Display("Displaying an exception using the ToString method: ");
                Display(sE.ToString());
            }
        }

        void DemandDeniedPermission()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The denied permission is: " + 
                    ((PermissionSet)sE.DenySetInstance).ToString());
                Display("The demanded permission is: " + 
                    sE.Demanded.ToString());
                Display("The security action is: " + sE.Action.ToString());
                Display("The method is: " + sE.Method);
                Display(
                    "The permission state at the time of the exception was: " + 
                    sE.PermissionState);
                Display("The permission that failed was: " + 
                    (IPermission)sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());
                Display("Demonstrating the use of the GetObjectData method.");
                SerializationInfo si = new SerializationInfo(
                    typeof(EntryPoint), new FormatterConverter());
                sE.GetObjectData(si, 
                    new StreamingContext(StreamingContextStates.All));
                Display("The FirstPermissionThatFailed from the " +
                    "call to GetObjectData is: ");
                Display(si.GetString("FirstPermissionThatFailed"));
            }
        }

        void DemandRefusedPermission()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Import);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The refused permission set is: " + 
                    (sE.RefusedSet).ToString());
                Display("The exception message is: " + sE.Message);
                Display("The failed assembly is: " + 
                    sE.FailedAssemblyInfo.EscapedCodeBase);
                Display("The granted set is: \n" + sE.GrantedSet);
                Display("The permission that failed is: " + 
                    sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());
                Display("The source is: " + sE.Source);
            }
        }

        void DemandPermitOnly()
        {
            try
            {
                KeyContainerPermission kCP = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP.Demand();
            }
            catch (SecurityException sE)
            {
                Display("The permitted permission is: " +
                    ((PermissionSet)sE.PermitOnlySetInstance).ToString());
                Display("The demanded permission is: " + 
                    sE.Demanded.ToString());
                Display("The security action is: " + sE.Action.ToString());
                Display("The method is: " + sE.Method.ToString());
                Display(
                    "The permission state at the time of the exception was: " +
                    sE.PermissionState);
                Display("The permission that failed was: " +
                    (IPermission)sE.FirstPermissionThatFailed);
                Display("The permission type is: " + 
                    sE.PermissionType.ToString());

                //Demonstrate the SecurityException constructor by 
                // throwing the exception again.
                Display("Rethrowing the exception thrown as a result of a " + 
                    "PermitOnly security action.");
                throw new SecurityException(sE.Message, sE.DenySetInstance, 
                    sE.PermitOnlySetInstance, sE.Method, sE.Demanded, 
                    (IPermission)sE.FirstPermissionThatFailed);
            }
        }
        void Display(string line)
        {
            Console.WriteLine(line);
        }
    }
}

System.Object
   System.Exception
     System.SystemException
      System.Security.SecurityException

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 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft