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.

Namespace: System.Security
Assembly: mscorlib (in mscorlib.dll)

public void PermitOnly ()
public final void PermitOnly ()
public final function PermitOnly ()
Not applicable.

Exception typeCondition


There is already an active PermitOnly for the current frame.

PermitOnly is similar to Deny, in that both cause stack walks to fail when they would otherwise succeed. 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. The call to PermitOnly is effective until the calling code returns to its caller. Only one PermitOnly can be active on a frame. An attempt to call PermitOnly when an active PermitOnly exists on the frame results in a SecurityException. Call RevertPermitOnly or RevertAll to remove an active PermitOnly.

PermitOnly is ignored for a permission not granted because a demand for that permission will not succeed. However, 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. 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.

Notes to Inheritors: You cannot override this method.

The following code example shows the use of the PermitOnly method to limit access to all resources except for the resource specified by the current instance. This code example is part of a larger example provided for the RevertAll method.

// Grant access only to OwnClipboard.
Console.WriteLine("Granting permission only for OwnClipboard access.");
// Revert the PermitOnly with a call to RevertPermitOnly.
Console.WriteLine("Reverting the PermitOnly.");

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0