CodeAccessPermission.Deny 方法



此 API 现已过时。

防止处于调用堆栈较高位置的调用函数使用可以调用此方法来访问当前实例指定资源的代码。Prevents callers higher in the call stack from using the code that calls this method to access the resource specified by the current instance.

 virtual void Deny();
 void Deny();
public void Deny ();
public void Deny ();
[System.Obsolete("Deny is obsolete and will be removed in a future release of the .NET Framework. See for more information.")]
public void Deny ();
public void Deny ();
abstract member Deny : unit -> unit
override this.Deny : unit -> unit
member this.Deny : unit -> unit
Public Sub Deny ()




已存在一个用于当前帧的活动的 Deny()There is already an active Deny() for the current frame.



应仅使用 Deny 方法来防止完全受信任的代码意外访问资源。The Deny method should be used only to protect resources from accidental access by fully trusted code. 不应使用它来防止不受信任的代码有意滥用资源。It should not be used to protect resources from intentional misuse by untrusted code. 例如,如果方法 A 为权限发出 Deny,然后调用方法 B,则方法 B 可以通过发出 Deny 来 overtly 重写 AssertFor example, if method A issues a Deny for a permission and then calls method B, method B can overtly override the Deny by issuing an Assert. 调用的方法在堆栈中始终更高。The called method is always higher in the stack. 因此,如果方法 B 尝试访问受保护的资源,则安全系统会开始检查其权限,因为方法 B 是直接调用方,然后向下遍历堆栈,以确认堆栈中没有 DenyPermitOnly 低于。Therefore, if method B tries to access a protected resource, the security system begins checking for permissions with it because method B is the immediate caller, and then walks down the stack to confirm that there is no Deny or PermitOnly lower in the stack. 尝试访问资源的方法 B可通过使用 Assert 方法立即停止堆栈遍历。Method B, which is trying to access the resource, can stop the stack walk immediately by using the Assert method. 在这种情况下,永远不会发现通过方法 A (调用方法)放置在堆栈上的 DenyIn that case, the Deny placed on the stack by method A (the calling method) is never discovered.

此方法阻止调用堆栈中较高位置的调用方通过调用此方法的代码访问受保护的资源,即使这些调用方已被授予访问该方法的权限。This method prevents callers higher in the call stack from accessing the protected resource through the code that calls this method, even if those callers have been granted permission to access it. 调用堆栈通常表示为向下增长,因此调用堆栈中较高位置的方法调用堆栈中较低的调用方法。The call stack is typically represented as growing down, so that methods higher in the call stack call methods lower in the call stack.

Deny 可以限制程序员的责任,或帮助防止意外的安全问题,因为它有助于防止调用 Deny 的方法访问由拒绝的权限保护的资源。Deny can limit the liability of the programmer or help prevent accidental security issues because it helps prevent the method that calls Deny from being used to access the resource protected by the denied permission. 如果方法对权限调用 Deny,并且调用堆栈中的调用方调用了该权限的 Demand,则该安全检查在到达 Deny时将失败。If a method calls Deny on a permission, and if a Demand for that permission is invoked by a caller lower in the call stack, that security check will fail when it reaches the Deny.

Deny 的调用在调用代码返回到其调用方之前有效。The call to Deny is effective until the calling code returns to its caller. 帧上只有一个 Deny 可以处于活动状态。Only one Deny can be active on a frame. 当帧上存在活动 Deny 导致 SecurityException时,尝试调用 DenyAn attempt to call Deny when an active Deny exists on the frame results in a SecurityException. 调用 RevertDenyRevertAll 以删除活动 DenyCall RevertDeny or RevertAll to remove an active Deny. 对于未授予的权限,将忽略 Deny,因为对该权限的请求将不会成功。Deny is ignored for a permission not granted because a demand for that permission will not succeed.


不能重写此方法。You cannot override this method.