Updated: July 2008
Prevents callers higher in the call stack from using the code that calls this method to access the resource specified by the current instance.
Assembly: mscorlib (in mscorlib.dll)
The 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. For example, if method A issues a for a permission and then calls method B, method B can overtly override the by issuing an Assert. The called method is always higher in the stack. 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 or PermitOnly lower in the stack. Method B, which is trying to access the resource, can stop the stack walk immediately by using the Assert method. In that case, the 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.
can limit the liability of the programmer or help prevent accidental security issues because it helps prevent the method that calls from being used to access the resource protected by the denied permission. If a method calls 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 .
The call to is effective until the calling code returns to its caller. Only one can be active on a frame. An attempt to call when an active exists on the frame results in a SecurityException. Call RevertDeny or RevertAll to remove an active . is ignored for a permission not granted because a demand for that permission will not succeed.Notes to Inheritors:
You cannot override this method.
The following code example shows the use of the method to prevent access to the resource specified by the current instance. This code example is part of a larger example provided for the RevertAll method.
// Create a new UIPermission that allows access only to OwnClipboard. UIPermission clipboardPermission = new UIPermission(UIPermissionClipboard.OwnClipboard); // Deny access to OwnClipboard. Console.WriteLine("Denying access to OwnClipboard"); clipboardPermission.Deny(); // Demand access to files in the specified path. DemandOwnClipboardAccess(); // Revert the Deny. Console.WriteLine("Reverting the Deny."); CodeAccessPermission.RevertDeny(); DemandOwnClipboardAccess();
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.