我用特別的方法來創建沙箱:如何更改AppDomain的權限?
internal static class Helper
{
public static AppDomain CreateSandbox()
{
Contract.Ensures(Contract.Result<AppDomain>() != null);
var platform = Assembly.GetExecutingAssembly();
var name = platform.FullName + ": Sandbox " + Guid.NewGuid();
var setup = new AppDomainSetup { ApplicationBase = platform.Location };
var permissions = new PermissionSet(PermissionState.None);
permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, platform.Location));
var sandbox = AppDomain.CreateDomain(name, null, setup, permissions);
Contract.Assume(sandbox != null);
return sandbox;
}
}
當我使用創建的沙箱,我想改變它的權限:
sandbox = Security.Helper.CreateSandbox();
sandbox.SetupInformation.ApplicationBase = Path.GetDirectoryName(path);
sandbox.PermissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, path));
但是當我加載組件,我收到例外:
「請求類型 'System.Security.Permissions.FileIOPermission,mscorlib程序,版本= 4.0.0.0,文化=中性公鑰= b77a5c561934e089' 的許可失敗。」
如何更改AppDomain的權限AFTER它的創建?
是創造了充分的信任運行沙盒的代碼?或者至少有FileIOPermission本身? – JRoughan
是的,創建沙箱運行的代碼是完全信任的。 –