【发布时间】:2017-03-13 18:14:35
【问题描述】:
我正在 Visual Studio 中处理一个小型沙盒项目。这是我的代码:
namespace Andromeda.PCTools
{
public partial class Sandbox : MetroForm
{
private AppDomain sandbox;
public Sandbox()
{
InitializeComponent();
}
private void Sandbox_Load(object sender, EventArgs e)
{
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Applications|*.exe", ValidateNames = true, Multiselect = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
listBoxItems.Items.Add(ofd.FileName);
}
}
}
private void removeSelectedToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listBoxItems.SelectedItems.Count != 0)
{
while (listBoxItems.SelectedIndex != -1)
{
listBoxItems.Items.RemoveAt(listBoxItems.SelectedIndex);
}
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
AppDomainSetup setup = new AppDomainSetup();
Evidence ev = new Evidence();
//ev.AddHostEvidence(new Zone(SecurityZone.Internet));
PermissionSet internetPS = SecurityManager.GetStandardSandbox(ev);
setup.ApplicationBase = Path.GetFullPath(Application.StartupPath);
//StrongName fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();
sandbox = AppDomain.CreateDomain(listBoxItems.SelectedItem.ToString(), ev, setup, ps);
try
{
sandbox.ExecuteAssembly(listBoxItems.SelectedItem.ToString());
btnLoad.Enabled = false;
btnUnload.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("The following error occurred!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnUnload_Click(object sender, EventArgs e)
{
try
{
AppDomain.Unload(sandbox);
btnLoad.Enabled = true;
btnUnload.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show("The following error occurred!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
但我收到以下错误:
抛出异常:仙女座中的“System.Security.SecurityException” 4.0.exe ("请求类型'System.Security.Permissions.FileIOPermission, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 失败。”)
【问题讨论】:
-
得到错误在哪里?上面有很多不相关的代码,见MCVE
-
你尝试执行的程序集,是否涉及文件I/O?如果是这样,那真的不会发生吗?
-
我基本上只是想让 EXE 在无法更改(损害)设备的环境中运行
-
有点像沙盒
标签: c# sandbox environment