受限权限的AppDomain授予集发行 [英] Restricted Permission AppDomain Grant Set Issue

查看:248
本文介绍了受限权限的AppDomain授予集发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,动态编译剃刀模板到一个大会,我与
A组权限(无访问文件等)执行。



这工作对我们的发展计算机和我们的测试服务器(Windows 2008 IIS7 64 .NET 4)。但是,我们的生产服务器上(同一规格),它给人的错误:



加载此程序集将产生不同的补助金,其他情况下设置。(从HRESULT异常:0x80131401)



下面是代码: -

 公共静态SandboxContext创建(字符串pathToUntrusted,列表与LT;大会>引用)
{
AppDomainSetup adSetup =新AppDomainSetup();
adSetup.ShadowCopyFiles =真;
变种DIR =新DirectoryInfo的(pathToUntrusted);
字符串TEMPPATH = Path.Combine(Path.GetTempPath(),dir.Name +_shadow);
adSetup.CachePath = TEMPPATH;


//我们的沙箱需要访问该组件。
串易达= Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPathbin\\CommonInterfaces.WebPages.dll);
System.IO.File.Copy(易达,Path.Combine(pathToUntrustedCommonInterfaces.WebPages.dll),TRUE);
VAR BASEDIR = Path.GetFullPath(pathToUntrusted);
adSetup.ApplicationBase = BASEDIR;
adSetup.PrivateBinPath = BASEDIR;

adSetup.PartialTrustVisibleAssemblies =
新的String [] {
typeof运算(System.Web.WebPageTraceListener).Assembly.FullName,
的typeof(System.Web.Razor。 RazorEngineHost).Assembly.FullName};

//设置在AppDomain的权限。我们给执行//读取/发现其中不可信的代码被加载的位置权限和

的PermissionSet permSet =新的PermissionSet(PermissionState.None);
permSet.AddPermission(新的SecurityPermission(SecurityPermissionFlag.Execution));

//我们希望sandboxer集的强名称,这样我们就可以把它添加到完全信任列表中。
强名称fullTrustAssembly = typeof运算(Sandboxer).Assembly.Evidence.GetHostEvidence<&强名称GT;();

证据证据=新证据();

//现在,我们有我们需要创建的AppDomain中,让我们创建它的一切。
的AppDomain NEWDOMAIN = AppDomain.CreateDomain(沙箱,证据,adSetup,permSet,fullTrustAssembly);

对象句柄处理= Activator.CreateInstanceFrom(
NEWDOMAIN的typeof(Sandboxer).Assembly.ManifestModule.FullyQualifiedName,
typeof运算(Sandboxer).FullName
);
//展开新的域实例为在这一领域的基准,并用它来执行
//不可信的代码。
VAR newDomainInstance =(Sandboxer)handle.Unwrap();
返回新SandboxContext(NEWDOMAIN,newDomainInstance);
}



任何想法,为什么这将是不同的一台服务器上?我刚刚安装的破服务器上的所有优秀的Windows Update和它没有帮助



如果我改变的PermissionSet为: -

 的PermissionSet permSet =新的PermissionSet(PermissionState.Unrestricted); 



所有的代码工作(但有一个安全问题,推测)


< DIV CLASS =h2_lin>解决方案

当您尝试组装不同的权限集加载到现有的AppDomain两次这个错误通常发生。在$ 100万的问题是它是什么总成,什么AppDomain中。



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




  • 什么沙盒组件(如果有的话)被装载到编组,因为你的主要应用程序域?

  • 如果您有自己的服务器代码,它指定的 LoadOptimizationAttribute

  • 请问你的开发服务器和生产服务器使用不同的隔离级别?

  • 是否有任何其他共享某些组件的生产服务器上的应用程序?



您也可以尝试在服务器上安装远程调试运行时,连接调试到承载您的应用程序,并直接检查什么得到什么领域出现加载过程。您可能需要为SOS调试扩展。



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


I have some code that dynamically compiles a Razor templates into an Assembly which I execute with a set of permissions (no access to files, etc).

This works on our development computers and on our test server (Windows 2008 IIS7 x64 .NET 4). But on our production server (Same spec) it gives the error:

"Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)"

Here is the code: -

    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);
    }

Any ideas why it would be different on one server? I just installed all the outstanding windows update on the broken server and it did not help.

If I change the PermissionSet to: -

        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);

All the code works (but presumable with a security problem)

解决方案

This error usually happens when you try to load an assembly into an existing AppDomain two times with different set of permissions. The $1M question is what assembly it is, and what AppDomain.

I don't have a complete answer to that, but you can look into the following things:

  • What sandboxed assemblies (if any) get loaded into your main app domain because of marshalling?
  • If you have your own server code, does it specify LoadOptimizationAttribute?
  • Does your development server and your production server use different isolation levels?
  • Are there any other applications on the production server that share some of your assemblies?

You can also try to install remote debugging runtime on the server, attach debugger to the process that hosts your application, and check directly what gets loaded there in what domain. You may need SOS debugging extensions for that.

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

这篇关于受限权限的AppDomain授予集发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆