SecurityException 類別

定義

偵測到安全性錯誤時所擲回的例外狀況。The exception that is thrown when a security error is detected.

public ref class SecurityException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class SecurityException : SystemException
type SecurityException = class
    inherit SystemException
Public Class SecurityException
Inherits SystemException
繼承
SecurityException
屬性

備註

當呼叫端沒有存取資源所需的許可權時,就會擲回 SecurityException 例外狀況。A SecurityException exception is thrown when a caller does not have the permissions required to access a resource. 下列範例會具現化包含 UIPermission 物件的 PermissionSet 物件,以允許存取 UI 物件和剪貼簿,並使用 RegistryPermission 物件來防止登錄存取。The following example instantiates a PermissionSet object that includes a UIPermission object to allow access to UI objects and the Clipboard and a RegistryPermission object to prevent registry access. 呼叫 PermissionSet.PermitOnly 方法,表示不論指派給呼叫者的許可權為何,都適用這些許可權。The call to the PermissionSet.PermitOnly method means that these permissions will apply regardless of the permissions assigned to the caller. 因此,嘗試建立登錄機碼會擲回 SecurityExceptionAs a result, the attempt to create a registry key throws a SecurityException.

using Microsoft.Win32;
using System;
using System.Security;
using System.Security.Permissions;

public class Example
{
   public static void Main()
   {
      PermissionSet perms = new PermissionSet(null);
      perms.AddPermission(new UIPermission(PermissionState.Unrestricted));
      perms.AddPermission(new RegistryPermission(PermissionState.None));
      perms.PermitOnly();
      
      try {
          RegistryKey key = Registry.CurrentUser.CreateSubKey("MyCompany\\Applications");
          Console.WriteLine("Registry key: {0}", key.Name);
      }
      catch (SecurityException e) {
         Console.WriteLine("Security Exception:\n\n{0}", e.Message);      
      }
   }
}
// The example displays the following output:
//    Security Exception:
//    
//    Request for the permission of type 'System.Security.Permissions.RegistryPermission, 
//    mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Imports Microsoft.Win32
Imports System.Security
Imports System.Security.Permissions

Module Example
   Public Sub Main()
      Dim perms As New PermissionSet(CType(Nothing, PermissionSet))
      perms.AddPermission(New UIPermission(PermissionState.Unrestricted))
      perms.AddPermission(New RegistryPermission(PermissionState.None))
      perms.PermitOnly()
      
      Try 
          Dim key As RegistryKey = Registry.CurrentUser.CreateSubKey("MyCompany\\Applications")
          Console.WriteLine("Registry key: {0}", key.Name)
      Catch e As SecurityException
         Console.WriteLine("Security Exception:\n\n{0}", e.Message)      
      End Try
   End Sub
End Module
' The example displays the following output:
'    Security Exception:
'    
'    Request for the permission of type 'System.Security.Permissions.RegistryPermission, 
'    mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

SecurityException 使用 HRESULT COR_E_SECURITY,其值為0x8013150A。SecurityException uses the HRESULT COR_E_SECURITY, which has the value 0x8013150A.

如需 SecurityException 類別之實例的初始屬性值清單,請參閱特定的 SecurityException 的函式。For a list of the initial property values for an instance of the SecurityException class, see a specific SecurityException constructor.

建構函式

SecurityException()

使用預設屬性,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with default properties.

SecurityException(SerializationInfo, StreamingContext)

初始化具有序列化資料之 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with serialized data.

SecurityException(String)

使用指定的錯誤訊息,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with a specified error message.

SecurityException(String, AssemblyName, PermissionSet, PermissionSet, MethodInfo, SecurityAction, Object, IPermission, Evidence)

針對沒有足夠授權集所造成的例外狀況,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class for an exception caused by an insufficient grant set.

SecurityException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with a specified error message and a reference to the inner exception that is the cause of this exception.

SecurityException(String, Object, Object, MethodInfo, Object, IPermission)

針對此堆疊上 Deny 所造成例外狀況,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class for an exception caused by a Deny on the stack.

SecurityException(String, Type)

使用指定的錯誤訊息以及造成例外狀況擲回的權限類型,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with a specified error message and the permission type that caused the exception to be thrown.

SecurityException(String, Type, String)

使用指定的錯誤訊息、造成例外狀況擲回的權限類型以及權限狀態,初始化 SecurityException 類別的新執行個體。Initializes a new instance of the SecurityException class with a specified error message, the permission type that caused the exception to be thrown, and the permission state.

屬性

Action

取得或設定造成例外狀況的安全性動作。Gets or sets the security action that caused the exception.

Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)
Demanded

取得或設定失敗之要求的安全性權限、權限集或權限集集合。Gets or sets the demanded security permission, permission set, or permission set collection that failed.

DenySetInstance

取得或設定造成要求失敗之遭拒絕安全性權限、權限集或權限集集合。Gets or sets the denied security permission, permission set, or permission set collection that caused a demand to fail.

FailedAssemblyInfo

取得或設定失敗組件的相關資訊。Gets or sets information about the failed assembly.

FirstPermissionThatFailed

取得或設定要求失敗之權限集合或權限集集合中的第一個權限。Gets or sets the first permission in a permission set or permission set collection that failed the demand.

GrantedSet

取得或設定造成 SecurityException 之組件的授與權限集合。Gets or sets the granted permission set of the assembly that caused the SecurityException.

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(繼承來源 Exception)
Method

取得或設定與例外狀況關聯之方法的相關資訊。Gets or sets the information about the method associated with the exception.

PermissionState

取得或設定擲回例外狀況的權限狀態。Gets or sets the state of the permission that threw the exception.

PermissionType

取得或設定失敗的權限類型。Gets or sets the type of the permission that failed.

PermitOnlySetInstance

取得或設定權限、權限集或權限集的集合,其屬於造成安全性檢查失敗之僅限允許的堆疊框架。Gets or sets the permission, permission set, or permission set collection that is part of the permit-only stack frame that caused a security check to fail.

RefusedSet

取得或設定造成 SecurityException 之組件的遭拒絕權限集合。Gets or sets the refused permission set of the assembly that caused the SecurityException.

Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)
Url

取得或設定造成例外狀況之組件的 URL。Gets or sets the URL of the assembly that caused the exception.

Zone

取得或設定造成例外狀況之組件的區域。Gets or sets the zone of the assembly that caused the exception.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

使用 SerializationInfo 的相關資訊來設定 SecurityExceptionSets the SerializationInfo with information about the SecurityException.

GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回目前 SecurityException 的表示。Returns a representation of the current SecurityException.

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於

另請參閱