尝试通过安全透明方法访问安全关键方法失败

尝试使用安全透明方法’PayPal.UserAgentHeader.get_OperatingSystemFriendlyName()’来访问安全关键方法’System.Management.ManagementObjectSearcher..ctor(System.String)’失败。

Assembly 'PayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=null' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MethodAccessException: Attempt by security transparent method 'PayPal.UserAgentHeader.get_OperatingSystemFriendlyName()' to access security critical method 'System.Management.ManagementObjectSearcher..ctor(System.String)' failed. Assembly 'PayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=null' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted. 

此stackoverflow答案提到将[SecuritySafeCritical]属性添加到类中,但在这种情况下,正在播放的类位于通过NuGet加载的DLL中。

我可以使用任何全局设置来绕过此exception吗?

将以下标记添加到web.config:

      

托管服务上的服务器可能设置为中等信任级别。 ‘PayPalCoreSDK’要求您的应用程序以完全信任级别运行。

将其添加到assemblyinfo.cs

 // added because of security transparency change in framework 4.0 [assembly: SecurityRules(SecurityRuleSet.Level1)] 

这为我做了……

将wpftoolkit 3.5框架升级到4.6.1框架后,assemblyinfo.cs中的以下安全规则解决了以下问题:

 // added because of security transparency change in framework 4.0 [assembly: SecurityRules(SecurityRuleSet.Level1)] 

在我的情况下,当我在解决方案中管理NuGet包时,一些包覆盖了主网站项目中的System.Web.Mvc程序集版本绑定。 设置回4.0.0.0(我安装了5.0)。 我没有注意到更改,因为Mvc v4.0已经安装并可通过GAC访问。 退后一步

我正在开发一个棕色地块应用程序,在解决方案中有很多引用的项目。 一个项目设置为.NET 4.0而不是4.6.1,我认为可能是它,但这不是问题。 我不得不补充:

  [assembly:AllowPartiallyTrustedCallers] 

到包含“安全关键”方法的项目中的assembly.cs文件,在我添加之前它对我不满意

 using System.Security; 

这就是诀窍。

乔伊