AppDomain.SetAppDomainPolicy 方法
为此应用程序域确定安全策略级别。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Sub SetAppDomainPolicy ( _
domainPolicy As PolicyLevel _
)
用法
Dim instance As AppDomain
Dim domainPolicy As PolicyLevel
instance.SetAppDomainPolicy(domainPolicy)
public void SetAppDomainPolicy (
PolicyLevel domainPolicy
)
public:
virtual void SetAppDomainPolicy (
PolicyLevel^ domainPolicy
) sealed
public final void SetAppDomainPolicy (
PolicyLevel domainPolicy
)
public final function SetAppDomainPolicy (
domainPolicy : PolicyLevel
)
参数
- domainPolicy
安全策略级别。
异常
异常类型 | 条件 |
---|---|
domainPolicy 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
已经设置了安全策略级别。 |
|
试图对已卸载的应用程序域进行操作。 |
备注
在将程序集加载到 AppDomain 之前调用此方法,目的是使安全策略生效。
示例
Imports System
Imports System.Threading
Imports System.Security
Imports System.Security.Policy
Imports System.Security.Permissions
Class ADSetAppDomainPolicy
Overloads Shared Sub Main(args() As String)
' Create a new application domain.
Dim domain As AppDomain = System.AppDomain.CreateDomain("MyDomain")
' Create a new AppDomain PolicyLevel.
Dim polLevel As PolicyLevel = PolicyLevel.CreateAppDomainLevel()
' Create a new, empty permission set.
Dim permSet As New PermissionSet(PermissionState.None)
' Add permission to execute code to the permission set.
permSet.AddPermission(New SecurityPermission(SecurityPermissionFlag.Execution))
' Give the policy level's root code group a new policy statement based
' on the new permission set.
polLevel.RootCodeGroup.PolicyStatement = New PolicyStatement(permSet)
' Give the new policy level to the application domain.
domain.SetAppDomainPolicy(polLevel)
' Try to execute the assembly.
Try
' This will throw a PolicyException if the executable tries to
' access any resources like file I/Q or window creation.
domain.ExecuteAssembly("Assemblies\MyWindowsExe.exe")
Catch e As PolicyException
Console.WriteLine("PolicyException: {0}", e.Message)
End Try
AppDomain.Unload(domain)
End Sub 'Main
End Class 'ADSetAppDomainPolicy
using System;
using System.Threading;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
namespace AppDomainSnippets
{
class ADSetAppDomainPolicy
{
static void Main(string[] args)
{
// Create a new application domain.
AppDomain domain = System.AppDomain.CreateDomain("MyDomain");
// Create a new AppDomain PolicyLevel.
PolicyLevel polLevel = PolicyLevel.CreateAppDomainLevel();
// Create a new, empty permission set.
PermissionSet permSet = new PermissionSet(PermissionState.None);
// Add permission to execute code to the permission set.
permSet.AddPermission
(new SecurityPermission(SecurityPermissionFlag.Execution));
// Give the policy level's root code group a new policy statement based
// on the new permission set.
polLevel.RootCodeGroup.PolicyStatement = new PolicyStatement(permSet);
// Give the new policy level to the application domain.
domain.SetAppDomainPolicy(polLevel);
// Try to execute the assembly.
try
{
// This will throw a PolicyException if the executable tries to
// access any resources like file I/O or tries to create a window.
domain.ExecuteAssembly("Assemblies\\MyWindowsExe.exe");
}
catch(PolicyException e)
{
Console.WriteLine("PolicyException: {0}", e.Message);
}
AppDomain.Unload(domain);
}
}
}
using namespace System;
using namespace System::Threading;
using namespace System::Security;
using namespace System::Security::Policy;
using namespace System::Security::Permissions;
int main()
{
// Create a new application domain.
AppDomain^ domain = System::AppDomain::CreateDomain( "MyDomain" );
// Create a new AppDomain PolicyLevel.
PolicyLevel^ polLevel = PolicyLevel::CreateAppDomainLevel();
// Create a new, empty permission set.
PermissionSet^ permSet = gcnew PermissionSet( PermissionState::None );
// Add permission to execute code to the permission set.
permSet->AddPermission( gcnew SecurityPermission( SecurityPermissionFlag::Execution ) );
// Give the policy level's root code group a new policy statement based
// on the new permission set.
polLevel->RootCodeGroup->PolicyStatement = gcnew PolicyStatement( permSet );
// Give the new policy level to the application domain.
domain->SetAppDomainPolicy( polLevel );
// Try to execute the assembly.
try
{
// This will throw a PolicyException if the executable tries to
// access any resources like file I/O or tries to create a window.
domain->ExecuteAssembly( "Assemblies\\MyWindowsExe.exe" );
}
catch ( PolicyException^ e )
{
Console::WriteLine( "PolicyException: {0}", e->Message );
}
AppDomain::Unload( domain );
}
.NET Framework 安全性
- SecurityPermission 以使用此方法。关联的枚举:SecurityPermissionFlag.ControlPolicy。
平台
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