Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AppDomain.SetAppDomainPolicy Method

Establishes the security policy level for this application domain.

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

Parameters

domainPolicy
The security policy level.

Implements

_AppDomain.SetAppDomainPolicy

Exceptions

Exception Type Condition
ArgumentNullException domainPolicy is a null reference (Nothing in Visual Basic).
PolicyException The security policy level has already been set.
AppDomainUnloadedException Operations are attempted on an unloaded application domain.
SecurityException The caller does not have the correct permissions. See the requirements section.

Remarks

Call this method before an assembly is loaded into the AppDomain in order for the security policy to have effect.

Example

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft