CodeAccessPermission.PermitOnly CodeAccessPermission.PermitOnly CodeAccessPermission.PermitOnly CodeAccessPermission.PermitOnly Method


防止呼叫堆疊中較高層的呼叫端,使用呼叫這個方法的程式碼來存取除了目前執行個體所指定資源之外的所有資源。Prevents callers higher in the call stack from using the code that calls this method to access all resources except for the resource specified by the current instance.

 virtual void PermitOnly();
public void PermitOnly ();
abstract member PermitOnly : unit -> unit
override this.PermitOnly : unit -> unit
Public Sub PermitOnly ()



目前的框架已有作用中的 PermitOnly()There is already an active PermitOnly() for the current frame.



PermitOnly方法應該只能用來防止意外存取的資源由完全信任的程式碼。The PermitOnly 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問題PermitOnly進行權限,然後呼叫方法B,方法Bovertly 可以覆寫PermitOnly發出AssertFor example, if method A issues a PermitOnly for a permission and then calls method B, method B can overtly override the PermitOnly 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. 在此情況下,PermitOnly放置在堆疊上方法A永遠不會探索 (呼叫的方法)。In that case, the PermitOnly placed on the stack by method A (the calling method) is never discovered.

PermitOnly 類似於Deny,兩者會導致堆疊查核行程,它們本來可以成功時失敗。PermitOnly is similar to Deny, in that both cause stack walks to fail when they would otherwise succeed. 其差異在於Deny指定將會導致失敗,堆疊查核行程的權限,但PermitOnly指定唯一的權限不會造成失敗的堆疊查核行程。The difference is that Deny specifies permissions that will cause the stack walk to fail, but PermitOnly specifies the only permissions that do not cause the stack walk to fail.

呼叫此方法,以確保您的程式碼,可用來存取指定的資源。Call this method to ensure that your code can be used to access only the specified resources. 若要呼叫PermitOnly會生效,直到呼叫程式碼傳回至呼叫端。The call to PermitOnly is effective until the calling code returns to its caller. 只有一個PermitOnly作用中的框架。Only one PermitOnly can be active on a frame. 嘗試呼叫PermitOnly當作用PermitOnly中的框架結果存在於SecurityExceptionAn attempt to call PermitOnly when an active PermitOnly exists on the frame results in a SecurityException. 呼叫RevertPermitOnly或是RevertAll若要移除作用PermitOnlyCall RevertPermitOnly or RevertAll to remove an active PermitOnly.

PermitOnly 會忽略該權限的要求將會失敗,因為未授與權限。PermitOnly is ignored for a permission not granted because a demand for that permission will not succeed. 不過,如果程式碼呼叫較低堆疊稍後呼叫Demand該權限,請SecurityException堆疊查核行程達到嘗試呼叫的程式碼時,會擲回PermitOnlyHowever, if code lower on the call stack later calls Demand for that permission, a SecurityException is thrown when the stack walk reaches the code that tried to call PermitOnly. 這是因為程式碼,呼叫PermitOnly沒有被授予權限,即使它呼叫PermitOnly該使用權限。This is because the code that called PermitOnly has not been granted the permission, even though it called PermitOnly for that permission. 通常,向下成長,來表示呼叫堆疊,使方法呼叫堆疊中較高層呼叫方法較低的呼叫堆疊中。The call stack is typically represented as growing down, so that methods higher in the call stack call methods lower in the call stack.


您無法覆寫這個方法。You cannot override this method.