I'm trying understand Sandboxing in .Net4.0 but stumbled over this problem and unsure how to get around it within a C# Windows Form application.
Within solution explorer I have 2 projects. The first project simply contains a Winform with a single button on it. When I click the button, the code is meant to call the second project and open up an OpenFileDialog
control. I'm not doing any file reads at all..just trying to simple display of the OpenFileDialog
control.
I am running under administrator priveleges but still receiving the following error message:
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Help would be appreciated, here's the actual code:
//Project 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using ClassLibrary1;
namespace DeleteSandboxing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//STEP1 - Setup the "PermissionSet"
PermissionSet permSet = new PermissionSet(PermissionState.None);
permSet.AddPermission(newSecurityPermission
(SecurityPermissionFlag.Execution));
permSet.AddPermission(new UIPermission(UIPermissionWindow.AllWindows));
permSet.AddPermission(new
FileDialogPermission(FileDialogPermissionAccess.Open));
//STEP2 - Setup the "AppDomainSetup"
AppDomainSetup objSetup = new AppDomainSetup();
objSetup.ApplicationBase =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//STEP3 - Create the "AppDomain"
AppDomain domain = AppDomain.CreateDomain("New domain name",
AppDomain.CurrentDomain.Evidence, objSetup, permSet);
//STEP4 - Call "ShowDialog()" via the interface i1.
Interface1 i1 =
(ClassLibrary1.Class1)domain.CreateInstanceFromAndUnwrap("ClassLibrary1",
"ClassLibrary1.Class1");
i1.ShowDialog();
}
}
}
//Project 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Reflection;
namespace ClassLibrary1
{
public class Class1:MarshalByRefObject,Interface1
{
public void ShowDialog()
{
OpenFileDialog obj = new OpenFileDialog();
obj.ShowDialog();
}
}
public interface Interface1
{
void ShowDialog();
}
}