2012-06-29 42 views
4

我有一些代码,动态编译一个Razor模板到一个程序集中,我使用 执行一组权限(无法访问文件等)。限制许可AppDomain授权集问题

这适用于我们的开发计算机和我们的测试服务器(Windows 2008 IIS7 x64 .NET 4)。但是,我们的生产服务器上(同一规格),它给人的错误:

这里是代码“加载此程序集将产生不同的补助金,其他情况下设置(从HRESULT异常0x80131401)。” -

public static SandboxContext Create(string pathToUntrusted, List<Assembly> references) 
    { 
     AppDomainSetup adSetup = new AppDomainSetup(); 
     adSetup.ShadowCopyFiles = "true"; 
     var dir = new DirectoryInfo(pathToUntrusted); 
     String tempPath = Path.Combine(Path.GetTempPath(), dir.Name + "_shadow");    
     adSetup.CachePath = tempPath; 


     // Our sandbox needs access to this assembly. 
     string AccessPath = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "bin\\CommonInterfaces.WebPages.dll"); 
     System.IO.File.Copy(AccessPath, Path.Combine(pathToUntrusted, "CommonInterfaces.WebPages.dll"), true); 
     var baseDir = Path.GetFullPath(pathToUntrusted); 
     adSetup.ApplicationBase = baseDir; 
     adSetup.PrivateBinPath = baseDir; 

     adSetup.PartialTrustVisibleAssemblies = 
      new string[] { 
       typeof(System.Web.WebPageTraceListener).Assembly.FullName, 
       typeof(System.Web.Razor.RazorEngineHost).Assembly.FullName}; 

     //Setting the permissions for the AppDomain. We give the permission to execute and to 
     //read/discover the location where the untrusted code is loaded. 
     PermissionSet permSet = new PermissionSet(PermissionState.None); 
     permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); 

     //We want the sandboxer assembly's strong name, so that we can add it to the full trust list. 
     StrongName fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>(); 

     Evidence evidence = new Evidence(); 

     //Now we have everything we need to create the AppDomain, so let's create it. 
     AppDomain newDomain = AppDomain.CreateDomain("Sandbox", evidence, adSetup, permSet, fullTrustAssembly); 

     ObjectHandle handle = Activator.CreateInstanceFrom(
      newDomain, typeof(Sandboxer).Assembly.ManifestModule.FullyQualifiedName, 
      typeof(Sandboxer).FullName 
      ); 
     //Unwrap the new domain instance into a reference in this domain and use it to execute the 
     //untrusted code. 
     var newDomainInstance = (Sandboxer)handle.Unwrap(); 
     return new SandboxContext(newDomain, newDomainInstance); 
    } 

任何想法,为什么它会在一台服务器上不同?我只是在破损的服务器上安装了所有优秀的Windows Update,并没有帮助。

如果我改变的PermissionSet到: -

 PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted); 

所有的代码工作(但出现安全漏洞推测)

回答

1

这个错误通常,当您试图将一个程序集加载到现有的AppDomain发生两次使用不同的权限集。 $ 1M的问题是它是什么组合,以及什么AppDomain。

我没有一个完整的答案,但你可以看看下面的事情:

  • 什么沙盒组件(如果有的话)得到装入因为编组你的主要应用领域?
  • 如果您有自己的服务器代码,它是否指定LoadOptimizationAttribute
  • 您的开发服务器和生产服务器是否使用不同的隔离级别?
  • 生产服务器上是否有任何其他应用程序共享部分程序集?

您还可以尝试在服务器上安装远程调试运行时,将调试器附加到承载应用程序的进程,并直接检查在哪个域中加载的内容。你可能需要SOS调试扩展。

http://msdn.microsoft.com/en-us/library/bb190764.aspx