Asp.net 类型违反继承sec规则:';System.Net.Http.WebRequestHandler';

Asp.net 类型违反继承sec规则:';System.Net.Http.WebRequestHandler';,asp.net,owin,typeloadexception,Asp.net,Owin,Typeloadexception,我正在尝试通过graph API访问Active Directory数据。我在运行应用程序时遇到以下错误 说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:“System.TypeLoadException:类型违反继承安全规则:'System.Net.Http.WebRequestHandler'。派生类型必须与基类型的安全可访问性匹配,或者不易访问。” 源错误: [TypeLoadException: Inherita

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

说明:执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

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

源错误:

[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 
在执行当前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 
由于我是初学者,我被这个错误深深打动了。有谁能告诉我需要做些什么来克服这个错误

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


...

注意,安装新的无关NuGet包时,bindingRedirect更改可能会被覆盖,您需要再次将绑定设置为4.0.0.0。

我用Visual Studio
NuGet Package Manager
解决了GitHub()上的漏洞,并且发布了新版本的
System.Net.Http


我能够用NuGet将
System.Net.Http
升级到
4.3.1版
,解决了这个问题。

我在使用.Net核心web应用程序(.Net framework)时遇到了问题。我已经尝试了我读到的所有东西,但只有从.net 4.6.1迁移到.net 4.5.1才有帮助。

我发现通过
Nuget
升级
System.net.Http
可以解决问题

您解决了吗?该问题已在2月22日之后的System.Net.Http版本4.3.1中解决2017@RobVonNesselrode,请注意,版本4.3.1仅在2017年2月22日之后才可用。我同意@Rob Von Nesselrode这是正确答案。此处相同,项目为.Net 4.6.1,出现错误,Nuget将System.Net.Http从4.3.0更新为4.3.4,已修复