简体   繁体   English

请求失败的.net程序集

[英]Request failed .net assembly

I have an application that uses plugins. 我有一个使用插件的应用程序。 The assembly chain looks like this: 组装链如下所示:

App -> A -> B -> A -> B 应用-> A-> B-> A-> B

The assemblies are "App" (the app being run), "A" - a linked in assembly and "B" - an assembly loaded with either Assembly.LoadFile or Assembly.LoadFrom (get the same error with both methods). 程序集是“ App”(正在运行的应用程序),“ A”(程序集中的链接)和“ B”-用Assembly.LoadFile或Assembly.LoadFrom加载的程序集(这两种方法都会产生相同的错误)。

It is throwing a security exception trying to LinkDemand unrestricted permissions. 它试图向LinkDemand授予不受限制的权限时引发了安全异常。 The exception happens when A calls B the second time. 当A第二次呼叫B时,发生例外。 B is NOT a signed assembly (and therefore I gather from what I've read not a strong named assembly). B 不是经过签名的程序集(因此,我从所读的内容中收集的不是强命名程序集)。 I have also tried putting the [assembly: AllowPartiallyTrustedCallers] attribute in the attribute.cs file for B (and tried putting it on all assemblies). 我还尝试过将[assembly:AllowPartiallyTrustedCallers]属性放在B的attribute.cs文件中(并尝试将其放在所有程序集中)。 I have even tried putting a permission assert for unrestricted permissions, though from what I've read that should have no effect on a LinkDemand. 我什至尝试将权限断言放到不受限制的权限中,尽管据我了解,这对LinkDemand无效。

I am aware that the assembly has reduced permissions (guessing this has something to do with loading it via Assembly). 我知道程序集的权限减少了(猜测这与通过程序集加载有关)。 However, everything I've read seems to indicate that LinkDemand only checks the calling permissions, so I'm not sure why it would fail the second time instead of the first. 但是,我阅读的所有内容似乎都表明LinkDemand仅检查调用权限,因此我不确定为什么第二次而不是第一次会失败。

I am confused because: a. 我很困惑,因为: why doesn't it fail the first time, b. 为什么它第一次失败? why does this happen at all (not using a strong named assembly afaik), and c. 为什么会发生这种情况(不使用强命名程序集afaik),以及c。 why doesn't adding the APTC attribute fix things. 为什么不添加APTC属性可以解决问题。

Just to be clear, I have no security demand attributes on any methods in any of the assemblies (that includes no LinkDemands). 需要明确的是,我在任何程序集中的任何方法上都没有安全需求属性(不包含LinkDemands)。 I do have some permission asserts in assembly A but I don't see how that could affect anything. 我在程序集A中确实有一些权限断言,但我看不到这会如何影响任何东西。

[System.Security.SecurityException] {"Request failed."} System.Security.SecurityException
Action                              LinkDemand  System.Security.Permissions.SecurityAction
Demanded                            {<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true"/> }   object {System.Security.PermissionSet}
DenySetInstance                     null    object
FailedAssemblyInfo                  {B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}  System.Reflection.AssemblyName
FirstPermissionThatFailed           null    System.Security.IPermission
GrantedSet                          "<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\">\r\n<IPermission class=\"System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nAccess=\"Open\"/>\r\n<IPermission class=\"System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nAllowed=\"ApplicationIsolationByUser\"\r\nUserQuota=\"512000\"/>\r\n<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nFlags=\"Execution\"/>\r\n<IPermission class=\"System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nWindow=\"SafeTopLevelWindows\"\r\nClipboard=\"OwnClipboard\"/>\r\n<IPermission class=\"System.Security.Permissions.UrlIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nUrl=\"file:///Z:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B.dll\"/>\r\n<IPermission class=\"System.Security.Permissions.ZoneIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nZone=\"Internet\"/>\r\n<IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"\r\nversion=\"1\"\r\nLevel=\"SafePrinting\"/>\r\n</PermissionSet>\r\n" string
Method                              null    System.Reflection.MethodInfo
PermissionState                     "<PermissionSet class=\"System.Security.PermissionSet\"\r\nversion=\"1\"\r\nUnrestricted=\"true\"/>\r\n"    string
PermissionType                      {Name = "PermissionSet" FullName = "System.Security.PermissionSet"} System.Type {System.RuntimeType}
PermitOnlySetInstance               null    object
RefusedSet                          ""  string
Url                                 "file:///Z:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B.dll"   string
Zone                                Internet    System.Security.SecurityZone

As suggested by Hans Passant, this appears to have something to do with the security zone being internet. 正如汉斯·帕桑特(Hans Passant)所建议的那样,这似乎与互联网安全区域有关。 I have since tried the following caspol commands to no avail: 此后,我尝试了以下caspol命令无济于事:

caspol -m -ag 1.2 -url file://z:/* FullTrust
caspol -m -ag 1.2 -url file://\\sam-crazycasta.dyndns.org/crazycasta/* FullTrust
caspol -m -ag 1.3 -url file://z:/* FullTrust
caspol -m -ag 1.3 -url file://\\sam-crazycasta.dyndns.org/crazycasta/* FullTrust

However, copying files local does work. 但是,在本地复制文件确实可以。 I would like a general solution that allows me to run from network drives and the like. 我想要一个通用的解决方案,允许我从网络驱动器等运行。 Preferably something I can do from within the program so the user doesn't need to know anything's wrong. 最好是我可以在程序中执行的操作,因此用户不需要知道任何错误。

Edit 1: I grabbed some code from MSDN to print out policies, and this is what I get, looks to me like FullTrust should be getting granted (the last line is the problem exception): 编辑1:我从MSDN中获取了一些代码以打印出策略,这就是我得到的,在我看来,应该授予FullTrust(最后一行是问题异常):

Policy Level Enterprise:
    FullTrust
    LocalIntranet
    Internet
    SkipVerification
    Execution
    Nothing
    Everything
Policy Level Machine:
    Nothing
    FullTrust
    LocalIntranet
    Internet
    SkipVerification
    Execution
    Everything
Policy Level User:
    FullTrust
    LocalIntranet
    Internet
    SkipVerification
    Execution
    Nothing
    Everything
A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

This print out is exactly the same whether run from the network where it fails, or from the hard drive where it works. 无论从失败的网络运行还是从工作的硬盘驱动器运行,此打印输出都完全相同。

I also tried adding the following function and calling it before the call that fails: 我还尝试添加以下函数并在失败的调用之前调用它:

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.LinkDemand, Unrestricted = true)]
public void dummy() {}

Since it's link demanding unrestricted permissions, I don't see why this function works and the other does not. 由于它的链接要求无限制的权限,因此我看不到为什么该功能起作用,而另一个则不起作用。

I was trying to kill a process in assembly B. This is only a partial answer. 我试图杀死程序集B中的一个进程。这只是部分答案。 I am still confused because the debugger pointed at the line in assembly A that called assembly B instead of the line in assembly B where I tried to kill the process. 我仍然感到困惑,因为调试器指向的是程序集A中称为程序集B的行,而不是我试图终止该进程的程序集B中的行。 Here is some code that demonstrates the problem, if someone can tell me how I'm supposed to know the exception is coming from assembly B I'd accept that as an answer. 这是一些演示该问题的代码,如果有人可以告诉我我应该如何知道异常来自程序集B,我将以此为答案。

Assembly A: 大会A:

using System;
using System.Text;

namespace A
{
    public interface IA
    {
        void A();
        void B();
    }

    public class A
    {
        public IA a;

        public A(IA _a)
        {
            a = _a;
        }

        public void callA()
        {
            Console.WriteLine("callA called, calling A.");
            a.A();
        }

        public void callB()
        {
            Console.WriteLine("callB called, calling B.");
            a.B();
        }
    }
}

Assembly B: 大会B:

using System;
using System.Text;

using System.Diagnostics;

namespace B
{
    public class _B : A.IA
    {
        public A.A a;

        public _B(A.A _a)
        {
            a = _a;
        }

        #region IA Members

        public void A()
        {
            Console.WriteLine("A called, calling callB.");
            a.callB();
        }

        public void B()
        {
            Console.WriteLine("B called.");
            Process.GetCurrentProcess().Kill();
        }

        #endregion
    }
}

App: 应用程式:

using System;
using System.Text;

using System.Reflection;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            A.IA ia;
            Assembly b;
            Type tb;
            A.A a;

            a = new A.A(null);
            b = Assembly.LoadFrom("../../../B/bin/Debug/B.dll");
            tb = b.GetType("B._B");
            ia = (A.IA)tb.GetConstructor(new Type[] { typeof(A.A) }).Invoke(new object[]{a});
            a.a = ia;
            a.callA();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 旧版程序集引发“请求主体权限失败” - Legacy assembly throws “Request for principal permission failed” .net本地程序集加载因CAS策略而失败 - .net local assembly load failed with CAS policy SQL Server:“为程序集&#39;测试&#39;创建程序集失败,因为程序集&#39;测试&#39;格式错误或不是纯.NET程序集。” - SQL Server: “CREATE ASSEMBLY for assembly 'Test' failed because assembly 'Test' is malformed or not a pure .NET assembly.” ASP.NET Core 2.0请求失败 - ASP.NET Core 2.0 request failed 请求类型系统net dnspermission的许可失败 - request for the permission of type system net dnspermission failed ASP.NET Core 失败的 AJAX 发布请求 - ASP.NET Core Failed AJAX Post Request ASP.NET MVC 失败的 AJAX 发布请求 - ASP.NET MVC Failed AJAX Post Request 使用 CSOM PnP 框架和 .NET 6 获取“令牌请求失败” - Getting" Token request failed" with CSOM PnP Framework and .NET 6 将swagger.net下载到Web Api项目后,由于装配生成失败,出现错误 - After downloading swagger.net to web Api project I get error as Assembly generation failed ASP.NET Core:Docker 还原/构建未能包含项目中的第 3 方程序集 - ASP.NET Core: Docker restore/build failed to include the 3rd party assembly from project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM