SecurityException SecurityException SecurityException SecurityException Class

定义

检测到安全性错误时引发的异常。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
继承
属性

注解

如果调用方没有访问资源所需的权限,则会引发异常。SecurityExceptionA SecurityException exception is thrown when a caller does not have the permissions required to access a resource. 下面的示例实例化PermissionSet一个对象,该UIPermission对象包含一个对象,以允许访问 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使用 COR_E_SECURITY 值为0x8013150A 的 HRESULT。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() SecurityException() SecurityException()

使用默认属性初始化 SecurityException 类的新实例。Initializes a new instance of the SecurityException class with default properties.

SecurityException(SerializationInfo, StreamingContext) SecurityException(SerializationInfo, StreamingContext) SecurityException(SerializationInfo, StreamingContext) SecurityException(SerializationInfo, StreamingContext)

用序列化数据初始化 SecurityException 类的新实例。Initializes a new instance of the SecurityException class with serialized data.

SecurityException(String) SecurityException(String) SecurityException(String) 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(String, AssemblyName, PermissionSet, PermissionSet, MethodInfo, SecurityAction, Object, IPermission, Evidence) SecurityException(String, AssemblyName, PermissionSet, PermissionSet, MethodInfo, SecurityAction, Object, IPermission, Evidence) 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(String, Exception) SecurityException(String, Exception) 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) SecurityException(String, Object, Object, MethodInfo, Object, IPermission) SecurityException(String, Object, Object, MethodInfo, Object, IPermission) 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(String, Type) SecurityException(String, Type) 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(String, Type, String) SecurityException(String, Type, String) 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 Action Action Action

获取或设置导致异常的安全操作。Gets or sets the security action that caused the exception.

Data Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
Demanded Demanded Demanded Demanded

获取或设置失败的要求的安全权限、权限集或权限集集合。Gets or sets the demanded security permission, permission set, or permission set collection that failed.

DenySetInstance DenySetInstance DenySetInstance DenySetInstance

获取或设置导致要求失败的被拒绝的安全权限、权限集或权限集集合。Gets or sets the denied security permission, permission set, or permission set collection that caused a demand to fail.

FailedAssemblyInfo FailedAssemblyInfo FailedAssemblyInfo FailedAssemblyInfo

获取或设置有关失败的程序集的信息。Gets or sets information about the failed assembly.

FirstPermissionThatFailed FirstPermissionThatFailed FirstPermissionThatFailed FirstPermissionThatFailed

获取或设置导致要求失败的权限集或权限集集合中的第一个权限。Gets or sets the first permission in a permission set or permission set collection that failed the demand.

GrantedSet GrantedSet GrantedSet GrantedSet

获取或设置导致 SecurityException 的程序集的被授予的权限集。Gets or sets the granted permission set of the assembly that caused the SecurityException.

HelpLink HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(Inherited from Exception)
Method Method Method Method

获取或设置关于与异常关联的方法的信息。Gets or sets the information about the method associated with the exception.

PermissionState PermissionState PermissionState PermissionState

获取或设置引发异常的权限的状态。Gets or sets the state of the permission that threw the exception.

PermissionType PermissionType PermissionType PermissionType

获取或设置失败权限的类型。Gets or sets the type of the permission that failed.

PermitOnlySetInstance PermitOnlySetInstance PermitOnlySetInstance 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 RefusedSet RefusedSet RefusedSet

获取或设置导致 SecurityException 的程序集的被拒绝的权限集。Gets or sets the refused permission set of the assembly that caused the SecurityException.

Source Source Source Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)
Url Url Url Url

获取或设置导致异常的程序集的 URL。Gets or sets the URL of the assembly that caused the exception.

Zone Zone Zone Zone

获取或设置导致异常的程序集区域。Gets or sets the zone of the assembly that caused the exception.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

通过有关 SerializationInfo 的信息设置 SecurityExceptionSets the SerializationInfo with information about the SecurityException.

GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回当前 SecurityException 的表示形式。Returns a representation of the current SecurityException.

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

适用于

另请参阅