无法加载类型“System.IdentityModel.Tokens.JwtSecurityToken”
Posted
技术标签:
【中文标题】无法加载类型“System.IdentityModel.Tokens.JwtSecurityToken”【英文标题】:Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' 【发布时间】:2018-01-07 05:27:28 【问题描述】:我有两个库。一个消耗另一个,而消耗库本身由 Web 应用程序使用。 Web 应用程序返回以下错误,而实例化类型的代码(掺杂 JWT 签名验证)在两个库的单元测试中成功执行。库,应用程序是目标框架 .NET 4.5.2 我验证了 Web 应用程序的 .csproj 文件具有有效的引用
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\System.IdentityModel.Tokens.Jwt.5.1.4\lib\net451\System.IdentityModel.Tokens.Jwt.dll</HintPath>
</Reference>
我在 GAC 中找不到要删除的程序集
C:\Program Files (x86)\Microsoft Visual Studio 11.0>gacutil /u System.IdentityMo
del.Tokens.Jwt
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.17929
Copyright (c) Microsoft Corporation. All rights reserved.
No assemblies found matching: System.IdentityModel.Tokens.Jwt
Number of assemblies uninstalled = 0
Number of failures = 0
我尝试过清理、重建、重新启动、删除 /bin 和 /lib 并重新构建,但不知所措。就个人而言,这可能不是 System.IdentityModel.Tokens.Jwt 本身,而是我需要的故障排除指导,例如类型加载错误。我已经专门重新安装了 .Tokens 包,实际上是所有包:
nuget install packages.config
我仍然收到错误,不是在构建时,而是在执行时:
Server Error in '/' Application.
Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
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.TypeLoadException: Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Source Error:
Line 12: protected void Application_Start(object sender, EventArgs e)
Line 13:
Line 14: GlobalConfiguration.Configure(WebApiConfig.Register);
Line 15:
Line 16:
Source File: C:\Dev\Git\Connect-Applications\ControllerlessApp\src\Connect.Rest.MainApp\Global.asax.cs Line: 14
Stack Trace:
[TypeLoadException: Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.]
System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes) +0
System.Reflection.RuntimeAssembly.GetExportedTypes() +31
Plex.Web.UI.ServiceResolverDefaults..ctor() +842
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +138
System.Activator.CreateInstance(Type type, Boolean nonPublic) +105
System.Activator.CreateInstance(Type type) +12
Plex.Activation.AssemblyDefaultRegistrationConvention.Process(Type type, Registry registry) +118
StructureMap.StringExtensions.Each(IEnumerable`1 enumerable, Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Extensions.cs:10
StructureMap.StringExtensions.Each(IEnumerable`1 enumerable, Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Extensions.cs:10
StructureMap.Graph.AssemblyScanner.ScanForAll(PluginGraph pluginGraph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\AssemblyScanner.cs:248
StructureMap.Graph.PluginGraph.Seal() in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\PluginGraph.cs:121
StructureMap.PluginGraphBuilder.Build() in c:\BuildAgent\work\767273992e840853\src\StructureMap\PluginGraphBuilder.cs:72
StructureMap.Container..ctor(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Container.cs:25
Plex.Activation.ServiceResolverBuilder.Initialize(ApplicationContext context) +288
Connect.Rest.Framework.Hosting2.Infrastructure.AppDomainSharedServices.get_ServiceResolver() +1373
Connect.Rest.Framework.Hosting2.WebApiConfig.Register(HttpConfiguration config) +80
System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +63
Connect.Rest.MainApp.WebApiApplication.Application_Start(Object sender, EventArgs e) in C:\Dev\Git\Connect-Applications\ControllerlessApp\src\Connect.Rest.MainApp\Global.asax.cs:14
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +544
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +186
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +402
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +343
[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +112
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +716
【问题讨论】:
您是否已将 jwt 库发布到 bin 文件夹? 谢谢@Gusman 我所做的是从基础库中获取所有 DLL,这些 DLL 实例化了这种类型,并对它们进行了所有必要的操作,并将它们复制到应用程序的 bin 中。这导致行为没有改变。 我遇到了类似的问题,奇怪的是它在单元测试项目中有效,但在 web api 中运行时无效,两个项目都使用相同版本的System.IdentityModel.Tokens.Jwt
。在我的情况下,我不能只降级到 v4,因为我有其他需要 v5 的依赖项,这非常烦人。
【参考方案1】:
我已经通过将包 System.IdentityModel.Tokens.Jwt 降级到 4.0 解决了这个问题
GitHub 上有一个问题 https://github.com/aspnet/AspNetKatana/issues/76
【讨论】:
以上是关于无法加载类型“System.IdentityModel.Tokens.JwtSecurityToken”的主要内容,如果未能解决你的问题,请参考以下文章
Global.asax 中的“解析器错误消息:无法加载类型”
无法加载一种或多种请求的类型。无法加载文件或程序集“Microsoft.VisualStudio.Coverage.CoreLib.Net
无法加载类型“AjaxControlToolkit.Sanitizer.AntiXssSanitizerProvider”。