CodeAccessPermission.RevertAll 方法
导致当前框架先前的所有重写都被移除,不再有效。
**命名空间:**System.Security
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Sub RevertAll
用法
CodeAccessPermission.RevertAll
public static void RevertAll ()
public:
static void RevertAll ()
public static void RevertAll ()
public static function RevertAll ()
异常
异常类型 | 条件 |
---|---|
当前框架没有上一个 Assert、Deny 和 PermitOnly。 |
备注
如果当前框架没有重写(Assert、Deny 或 PermitOnly),则引发 ExecutionEngineException。
示例
下面的代码示例演示如何使用 RevertAll 方法来恢复以前对当前框架的重写。
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.IO
Class UIPermissions
Public Shared Sub Main()
Try
' Create a new UIPermission that allows access only to OwnClipboard.
Dim clipboardPermission As 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()
' Grant access only to OwnClipboard.
Console.WriteLine("Granting permission only for OwnClipboard access.")
clipboardPermission.PermitOnly()
DemandAllClipboardAccess()
' Revert the PermitOnly with a call to RevertPermitOnly.
Console.WriteLine("Reverting the PermitOnly.")
CodeAccessPermission.RevertPermitOnly()
DemandAllClipboardAccess()
' Permit access only to OwnClipboard.
clipboardPermission.PermitOnly()
DemandAllClipboardAccess()
' Revert the PermitOnly with a call to RevertAll.
Console.WriteLine("Reverting the PermitOnly using RevertAll.")
CodeAccessPermission.RevertAll()
DemandAllClipboardAccess()
Console.WriteLine("This sample completed successfully; " + _
"press Enter to exit.")
Console.ReadLine()
Catch e As Exception
Console.WriteLine("Unexpected exception thrown: " + vbLf + e.ToString())
Console.ReadLine()
End Try
End Sub 'Main
' Determine whether OwnClipboard can be accessed.
Private Shared Sub DemandOwnClipboardAccess()
Try
' Create a new UIPermission that allows access to OwnClipboard.
Dim clipboardPermission As New UIPermission(UIPermissionClipboard.OwnClipboard)
' Verify that callers higher in the stack have been granted
' the permission.
Console.WriteLine("Demanding OwnClipboard access.")
clipboardPermission.Demand()
Console.WriteLine("The demand was successful")
Catch ex As SecurityException
Console.WriteLine("A security exception was thrown while " + _
"demanding OwnClipboard access permission " + vbLf)
Console.WriteLine(ex.Message)
Catch ex As Exception
Console.WriteLine("A fatal exception occurred:" + vbLf + ex.ToString())
End Try
End Sub 'DemandOwnClipboardAccess
' Determine whether OwnClipboard can be accessed.
Private Shared Sub DemandAllClipboardAccess()
Try
' Create a new UIPermission that allows access to OwnClipboard.
Dim clipboardPermission As New UIPermission(UIPermissionClipboard.AllClipboard)
' Verify that callers higher in the stack have been granted
' the permission.
Console.WriteLine("Demanding AllClipboard access.")
clipboardPermission.Demand()
Console.WriteLine("The demand was successful")
Catch ex As SecurityException
Console.WriteLine("A security exception was thrown while " + _
"demanding AllClipboard access permission " + vbLf)
Console.WriteLine(ex.Message)
Catch ex As Exception
Console.WriteLine("A fatal exception occurred:" + vbLf + ex.ToString())
End Try
End Sub 'DemandAllClipboardAccess
End Class 'UIPermissions
using System;
using System.Security;
using System.Security.Permissions;
using System.IO;
class UIPermissions
{
public static void Main()
{
try
{
// 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();
// Grant access only to OwnClipboard.
Console.WriteLine("Granting permission only for OwnClipboard access.");
clipboardPermission.PermitOnly();
DemandAllClipboardAccess();
// Revert the PermitOnly with a call to RevertPermitOnly.
Console.WriteLine("Reverting the PermitOnly.");
CodeAccessPermission.RevertPermitOnly();
DemandAllClipboardAccess();
// Permit access only to OwnClipboard.
clipboardPermission.PermitOnly();
DemandAllClipboardAccess();
// Revert the PermitOnly with a call to RevertAll.
Console.WriteLine("Reverting the PermitOnly using RevertAll.");
CodeAccessPermission.RevertAll();
DemandAllClipboardAccess();
Console.WriteLine("This sample completed successfully; " +
"press Enter to exit.");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception thrown: \n" + e.ToString());
Console.ReadLine();
}
}
// Determine whether OwnClipboard can be accessed.
private static void DemandOwnClipboardAccess()
{
try
{
// Create a new UIPermission that allows access to OwnClipboard.
UIPermission clipboardPermission = new UIPermission(UIPermissionClipboard.OwnClipboard);
// Verify that callers higher in the stack have been granted
// the permission.
Console.WriteLine("Demanding OwnClipboard access.");
clipboardPermission.Demand();
Console.WriteLine("The demand was successful");
}
catch (SecurityException ex)
{
Console.WriteLine("A security exception was thrown while " +
"demanding OwnClipboard access permission \n");
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("A fatal exception occurred:\n" +
ex.ToString());
}
}
// Determine whether OwnClipboard can be accessed.
private static void DemandAllClipboardAccess()
{
try
{
// Create a new UIPermission that allows access to OwnClipboard.
UIPermission clipboardPermission = new UIPermission(UIPermissionClipboard.AllClipboard);
// Verify that callers higher in the stack have been granted
// the permission.
Console.WriteLine("Demanding AllClipboard access.");
clipboardPermission.Demand();
Console.WriteLine("The demand was successful");
}
catch (SecurityException ex)
{
Console.WriteLine("A security exception was thrown while " +
"demanding AllClipboard access permission \n");
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("A fatal exception occurred:\n" +
ex.ToString());
}
}
}
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::IO;
ref class UIPermissions
{
public:
static void Main()
{
try
{
// Create a new UIPermission that allows access only to OwnClipboard.
UIPermission^ clipboardPermission = gcnew UIPermission(
UIPermissionClipboard::OwnClipboard );
// Deny access to OwnClipboard.
Console::WriteLine( L"Denying access to OwnClipboard" );
clipboardPermission->Deny();
// Demand access to files in the specified path.
DemandOwnClipboardAccess();
// Revert the Deny.
Console::WriteLine( L"Reverting the Deny." );
CodeAccessPermission::RevertDeny();
DemandOwnClipboardAccess();
// Grant access only to OwnClipboard.
Console::WriteLine( L"Granting permission only for OwnClipboard access." );
clipboardPermission->PermitOnly();
DemandAllClipboardAccess();
// Revert the PermitOnly with a call to RevertPermitOnly.
Console::WriteLine( L"Reverting the PermitOnly." );
CodeAccessPermission::RevertPermitOnly();
DemandAllClipboardAccess();
// Permit access only to OwnClipboard.
clipboardPermission->PermitOnly();
DemandAllClipboardAccess();
// Revert the PermitOnly with a call to RevertAll.
Console::WriteLine( L"Reverting the PermitOnly using RevertAll." );
CodeAccessPermission::RevertAll();
DemandAllClipboardAccess();
Console::WriteLine( L"This sample completed successfully; "
L"press Enter to exit." );
Console::ReadLine();
}
catch ( Exception^ e )
{
Console::WriteLine( L"Unexpected exception thrown: \n{0}", e );
Console::ReadLine();
}
}
private:
// Determine whether OwnClipboard can be accessed.
static void DemandOwnClipboardAccess()
{
try
{
// Create a new UIPermission that allows access to OwnClipboard.
UIPermission^ clipboardPermission =
gcnew UIPermission( UIPermissionClipboard::OwnClipboard );
// Verify that callers higher in the stack have been granted
// the permission.
Console::WriteLine( L"Demanding OwnClipboard access." );
clipboardPermission->Demand();
Console::WriteLine( L"The demand was successful" );
}
catch ( SecurityException^ ex )
{
Console::WriteLine( L"A security exception was thrown while "
L"demanding OwnClipboard access permission \n" );
Console::WriteLine( ex->Message );
}
catch ( Exception^ ex )
{
Console::WriteLine( L"A fatal exception occurred:\n{0}", ex );
}
}
// Determine whether OwnClipboard can be accessed.
static void DemandAllClipboardAccess()
{
try
{
// Create a new UIPermission that allows access to OwnClipboard.
UIPermission^ clipboardPermission =
gcnew UIPermission( UIPermissionClipboard::AllClipboard );
// Verify that callers higher in the stack have been granted
// the permission.
Console::WriteLine( L"Demanding AllClipboard access." );
clipboardPermission->Demand();
Console::WriteLine( L"The demand was successful" );
}
catch ( SecurityException^ ex )
{
Console::WriteLine( L"A security exception was thrown while "
L"demanding AllClipboard access permission \n" );
Console::WriteLine( ex->Message );
}
catch ( Exception^ ex )
{
Console::WriteLine( L"A fatal exception occurred:\n{0}", ex );
}
}
};
int main()
{
UIPermissions::Main();
}
平台
Windows 98、Windows 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
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
CodeAccessPermission 类
CodeAccessPermission 成员
System.Security 命名空间