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 ref class SecurityException : public 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 namespace System;
using namespace System::Data;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Policy;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;

namespace SecurityExceptionSample
{
    [assembly:KeyContainerPermissionAttribute(
        SecurityAction::RequestRefuse,
        Flags=KeyContainerPermissionFlags::Import)];
    ref class TestSecurityException
    {
    public:
        void MakeTest()
        {
            try
            {
                // Deny a permission.
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->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^ permitOnlySet = gcnew PermissionSet(
                    PermissionState::None);
                permitOnlySet->AddPermission(gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Import));
                permitOnlySet->AddPermission(gcnew SecurityPermission(
                    SecurityPermissionFlag::ControlEvidence |
                    SecurityPermissionFlag::ControlPolicy |
                    SecurityPermissionFlag::SerializationFormatter));
                permitOnlySet->PermitOnly();
                Display("Demanding an implicitly refused permission. \n\n");
                DemandPermitOnly();
            }
            catch (SecurityException^ exception)
            {
                Display("*******************************************"
                    "**************\n");
                
                Display("Displaying an exception using the ToString "
                    "method: ");
                Display(exception->ToString());
            }

        }

    private:
        void DemandDeniedPermission()
        {
            try
            {
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->Demand();
            }
            catch (SecurityException^ exception)
            {
                Display("The denied permission is: {0}",
                    exception->DenySetInstance);

                Display("The demanded permission is: {0}",
                    exception->Demanded);

                Display("The security action is: {0}",
                    exception->Action);

                Display("The method is: {0}", exception->Method);

                Display("The permission state at the time "
                    "of the exception was: {0}",
                    exception->PermissionState);

                Display("The permission that failed was: {0}",
                    exception->FirstPermissionThatFailed);

                Display("The permission type is: {0}",
                    exception->PermissionType);

                Display("Demonstrating the use of the GetObjectData "
                    "method.");
                SerializationInfo^ entryPointSerializatonInfo =
                    gcnew SerializationInfo(TestSecurityException::typeid,
                    gcnew FormatterConverter);
                exception->GetObjectData(entryPointSerializatonInfo,
                    *gcnew StreamingContext(StreamingContextStates::All));
                Display("The FirstPermissionThatFailed from the call"
                    " to GetObjectData is: ");
                Display(entryPointSerializatonInfo->GetString(
                    "FirstPermissionThatFailed"));
            }
        }

        void DemandRefusedPermission()
        {
            try
            {
                KeyContainerPermission^ keyContainerImportPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Import);
                keyContainerImportPermission->Demand();
            }
            catch (SecurityException^ exception)
            {            
                Display("The refused permission set is: {0}",
                    exception->RefusedSet);

                Display("The exception message is: {0}",
                    exception->Message);

                Display("The failed assembly is: {0}",
                    exception->FailedAssemblyInfo->EscapedCodeBase);

                Display("The granted set is: \n{0}",
                    exception->GrantedSet);

                Display("The permission that failed is: {0}",
                    exception->FirstPermissionThatFailed);
                Display("The permission type is: {0}",
                    exception->PermissionType);

                Display("The source is: {0}", exception->Source);
            }
        }

        void DemandPermitOnly()
        {
            try
            {
                KeyContainerPermission^ keyContainerDecryptPermission =
                    gcnew KeyContainerPermission(
                    KeyContainerPermissionFlags::Decrypt);
                keyContainerDecryptPermission->Demand();
            }
            catch (SecurityException^ exception)
            {
                Display("The permitted permission is: {0}",
                    exception->PermitOnlySetInstance);
                Display("The demanded permission is: {0}",
                    exception->Demanded);
                Display("The security action is: {0}",
                    exception->Action);
                Display("The method is: {0}", exception->Method);
                Display("The permission state at the time of the "
                    "exception was: {0}", exception->PermissionState);
                Display("The permission that failed was: {0}",
                    exception->FirstPermissionThatFailed);
                Display("The permission type is: {0}",
                    exception->PermissionType);

                // Demonstrate the SecurityException constructor
                // by throwing the exception again.
                Display("Rethrowing the exception thrown as a "
                    "result of a PermitOnly security action.");
                throw gcnew SecurityException(exception->Message,
                    exception->DenySetInstance,
                    exception->PermitOnlySetInstance,
                    exception->Method, exception->Demanded,
                    exception->FirstPermissionThatFailed);
                }
        }

        void Display(String^ line)
        {
            Console::WriteLine(line);
        }

        void Display(String^ format, Object^ arg)
        {
            Console::WriteLine(format, arg);
        }
    };
}

using namespace SecurityExceptionSample;

int main()
{
    TestSecurityException^ test = gcnew TestSecurityException;
    test->MakeTest();
    Console::WriteLine("Press the enter key to exit.");
    Console::Read();
}

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