类型 'System.Net.Http.WebRequestHandler' 违反了继承安全规则

24

我正在尝试通过图形API访问Active Directory数据。在运行应用程序时,我遇到了以下错误。

描述:当前Web请求的执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误和代码中起源的更多信息。

异常详细信息:"System.TypeLoadException: 类型'System.Net.Http.WebRequestHandler'违反了继承安全性规则。派生类型必须匹配基类型的安全可访问性,或者比其低。"

源错误:

在执行当前Web请求时生成未处理的异常。可以使用下面的异常堆栈跟踪标识异常的起源和位置。

堆栈跟踪:

[TypeLoadException: Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.]
   Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationMiddleware.ResolveHttpMessageHandler(OpenIdConnectAuthenticationOptions options) +0
   Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationMiddleware..ctor(OwinMiddleware next, IAppBuilder app, OpenIdConnectAuthenticationOptions options) +996
   lambda_method(Closure , OwinMiddleware , IAppBuilder , OpenIdConnectAuthenticationOptions ) +54

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +92
   System.Delegate.DynamicInvokeImpl(Object[] args) +117
   System.Delegate.DynamicInvoke(Object[] args) +12
   Microsoft.Owin.Builder.AppBuilder.BuildInternal(Type signature) +236
   Microsoft.Owin.Builder.AppBuilder.Build(Type returnType) +21
   Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +565
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +58
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +95
   System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
   System.Threading.LazyInitializer.EnsureInitialized(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +72
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +96
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +523
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +364
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +770
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +195


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1069.1 

作为一个初学者我遇到了错误。请问有人能告诉我如何克服这个错误吗?


你解决了吗? - JoeBrockhaus
1
问题已在System.Net.Http版本4.3.1中解决,解决日期为2017年2月22日之后。 - Michael Freidgeim
5个回答

38

看起来这个bug现在已经在GitHub上被关闭了(https://github.com/dotnet/corefx/issues/11100),并且System.Net.Http的一个新版本已经发布。

我能够通过NuGet将System.Net.Http升级到4.3.1版本,问题得到了解决。


@RobVonNesselrode,请注意,4.3.1版本仅在2017年2月22日之后才可用。 - Michael Freidgeim
我同意@Rob Von Nesselrode的观点,这是正确的答案。 - Oleg

27

这对我起作用了: System.Net.Http v4.1.0.0 似乎存在一些问题。在 web.config 或 app.config 中,指向旧版本 (v4.0.0.0)。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     ...
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.0.0.0" />
        </dependentAssembly>

请注意,当您安装一个与之无关的新NuGet包时,bindingRedirect更改可能会被覆盖,需要再次将binding设置为4.0.0.0。


6

我发现通过Nuget升级System.Net.Http可以解决这个问题。


一样的问题,项目是 .Net 4.6.1,出现了错误,Nuget 将 System.Net.Http 从 4.3.0 更新到了 4.3.4,已经解决。 - Sammer

1

我通过使用Visual Studio的NuGet包管理器,将System.Net.Http4.3.0降级到4.0.0来解决了问题。


0

我在使用 .net core web 应用程序 (.net framework) 时遇到了问题。我尝试了这里所有的方法,但只有从 .net 4.6.1 迁移到 .net 4.5.1 才有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接