受限权限 AppDomain 授权集问题 [英] Restricted Permission AppDomain Grant Set Issue

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

问题描述

我有一些代码可以将 Razor 模板动态编译成我执行的程序集一组权限(不能访问文件等).

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).

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

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:

加载此程序集将产生与其他实例不同的授权集.(来自 HRESULT 的异常:0x80131401)"

"Loading this assembly would produce a different grant set from other instances. (Exception from 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 更新,但没有帮助.

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.

如果我将 PermissionSet 更改为:-

If I change the PermissionSet to: -

        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);

所有代码都有效(但可能存在安全问题)

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

推荐答案

当您尝试使用不同的权限集两次将程序集加载到现有 AppDomain 中时,通常会发生此错误.100 万美元的问题是它是什么程序集,以及什么 AppDomain.

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:

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

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天全站免登陆