4

Razor テンプレートを一連のアクセス許可 (ファイルへのアクセスなしなど) で実行するアセンブリに動的にコンパイルするコードがいくつかあります。

これは、開発用コンピューターとテスト サーバー (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);
    }

1 つのサーバーで異なる理由はありますか? 壊れたサーバーにすべての未解決の Windows 更新プログラムをインストールしましたが、役に立ちませんでした。

PermissionSet を次のように変更した場合: -

        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);

すべてのコードが機能します (ただし、セキュリティ上の問題があると思われます)

4

1 に答える 1