次の方法で共有


AppDomain.SetAppDomainPolicy メソッド

アプリケーション ドメインのセキュリティ ポリシー レベルを設定します。

Public Overridable Sub SetAppDomainPolicy( _
   ByVal domainPolicy As PolicyLevel _) Implements _AppDomain.SetAppDomainPolicy
[C#]
public virtual void SetAppDomainPolicy(PolicyLeveldomainPolicy);
[C++]
public: virtual void SetAppDomainPolicy(PolicyLevel* domainPolicy);
[JScript]
public function SetAppDomainPolicy(
   domainPolicy : PolicyLevel);

パラメータ

  • domainPolicy
    セキュリティ ポリシー レベル。

実装

_AppDomain.SetAppDomainPolicy

例外

例外の種類 条件
ArgumentNullException domainPolicy が null 参照 (Visual Basic では Nothing) です。
PolicyException セキュリティ ポリシー レベルが既に設定されています。
AppDomainUnloadedException 操作が、アンロードされたアプリケーション ドメインで試行されています。
SecurityException 呼び出し元に、正しいアクセス許可がありません。要件のセクションを参照してください。

解説

セキュリティ ポリシーを有効にするには、 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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>

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(S"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(S"Assemblies\\MyWindowsExe.exe");
   } catch (PolicyException* e) {
      Console::WriteLine(S"PolicyException: {0}", e->Message);
   }

   AppDomain::Unload(domain);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

AppDomain クラス | AppDomain メンバ | System 名前空間