다음을 통해 공유


SecurityException 클래스

보안 오류가 감지될 때 throw되는 예외입니다.

네임스페이스: System.Security
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class SecurityException
    Inherits SystemException
‘사용 방법
Dim instance As SecurityException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class SecurityException : SystemException
[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은 0x8013150A 값을 가지는 HRESULT COR_E_SECURITY를 사용합니다.

SecurityException 클래스의 인스턴스에 대한 초기 속성 값 목록을 보려면 특정 SecurityException 생성자를 참조하십시오.

예제

다음 코드 예제에서는 SecurityException 클래스의 멤버를 사용하는 방법을 보여 줍니다.

Imports System
Imports System.Data
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Policy
Imports System.Reflection
Imports System.Runtime.Serialization

<Assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse, _
    Flags:=KeyContainerPermissionFlags.Import)> 

Class EntryPoint

    <STAThread()> _
    Shared Sub Main()
        Dim eP As New EntryPoint()
        eP.RunCode()
        Console.WriteLine("Press the ENTER key to exit.")
        Console.Read()

    End Sub 'Main

    Sub RunCode()
        Try
            ' Deny a permission.
            Dim kCP1 As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP1.Deny()

            ' Demand the denied permission and display the 
            ' exception properties.
            Display("Demanding a denied permission. " & vbLf & vbLf)
            DemandDeniedPermission()
            Display("************************************************" & vbLf)
            CodeAccessPermission.RevertDeny()

            ' Demand the permission refused in the 
            ' assembly-level attribute.
            Display("Demanding a refused permission. " & vbLf & vbLf)
            DemandRefusedPermission()
            Display("************************************************" & vbLf)

            ' 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.
            Dim permitOnly As New PermissionSet(PermissionState.None)
            permitOnly.AddPermission( _
                New KeyContainerPermission(KeyContainerPermissionFlags.Import))
            permitOnly.AddPermission(New SecurityPermission( _
                SecurityPermissionFlag.ControlEvidence Or _ 
                SecurityPermissionFlag.ControlPolicy Or _
                SecurityPermissionFlag.SerializationFormatter))
            permitOnly.PermitOnly()
            Display( _
                "Demanding an implicitly refused permission. " & vbLf & vbLf)
            DemandPermitOnly()
        Catch sE As Exception
            Display("************************************************" & vbLf)
            Display("Displaying an exception using the ToString method: ")
            Display(sE.ToString())
        End Try
    End Sub 'RunCode

    Sub DemandDeniedPermission()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP.Demand()
        Catch sE As SecurityException
            Display("The denied permission is: " & _
                CType(sE.DenySetInstance, PermissionSet).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: " & _
                CType(sE.FirstPermissionThatFailed, _
                IPermission).ToXml().ToString())
            Display("The permission type is: " & sE.PermissionType.ToString())
            Display("Demonstrating the use of the GetObjectData method.")
            Dim si As New SerializationInfo( _
                GetType(EntryPoint), New FormatterConverter())
            sE.GetObjectData(si, _
                New StreamingContext(StreamingContextStates.All))
            Display("The FirstPermissionThatFailed from the " & _
                "call to GetObjectData is: ")
            Display(si.GetString("FirstPermissionThatFailed"))
        End Try
    End Sub 'DemandDeniedPermission

    Sub DemandRefusedPermission()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Import)
            kCP.Demand()
        Catch sE As SecurityException
            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: " & vbLf & sE.GrantedSet)
            Display("The permission that failed is: " & CType( _
                sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
            Display("The permission type is: " & sE.PermissionType.ToString())
            Display("The source is: " & sE.Source)
        End Try
    End Sub 'DemandRefusedPermission

    Sub DemandPermitOnly()
        Try
            Dim kCP As New KeyContainerPermission( _
                KeyContainerPermissionFlags.Decrypt)
            kCP.Demand()
        Catch sE As SecurityException
            Display("The permitted permission is: " & _
                CType(sE.PermitOnlySetInstance, PermissionSet).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: " & CType( _
                sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
            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, _
                CType(sE.FirstPermissionThatFailed, IPermission))
        End Try

    End Sub 'DemandPermitOnly

    Sub Display(ByVal line As String)

        Console.WriteLine(line)

    End Sub 'Display
End Class 'EntryPoint
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);
        }
    }
}
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

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

SecurityException 멤버
System.Security 네임스페이스
Exception

기타 리소스

예외 처리 및 Throw