违反类型的继承sec规则:'System.Net.Http.WebRequestHandler'。

时间:2016-07-28 02:48:54

标签: azure-ad-graph-api

我正在尝试通过图形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 

由于我是初学者,我对这个错误很感兴趣。任何人都可以告诉我需要做些什么来克服这个错误

5 个答案:

答案 0 :(得分:37)

似乎该错误已在GitHub(https://github.com/dotnet/corefx/issues/11100)上关闭,并且已发布新版System.Net.Http

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

答案 1 :(得分:25)

这对我有用: 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更改可能会被覆盖,您需要再次将绑定设置为4.0.0.0。

答案 2 :(得分:4)

我发现通过System.Net.Http升级Nuget修复了它。

答案 3 :(得分:1)

我解决了使用Visual Studio System.Net.Http4.3.04.0.0降级为NuGet Package Manager的问题

答案 4 :(得分:0)

我遇到了使用.net核心Web应用程序(.net框架)的问题。我已经尝试了所有我读过的内容,但只是从.net 4.6.1迁移到.net 4.5.1。