在WebAPI2项目中加载System.IdentityModel.Tokens.Jwt dll时出错

编程入门 行业动态 更新时间:2024-10-19 02:17:50
本文介绍了在WebAPI2项目中加载System.IdentityModel.Tokens.Jwt dll时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在WebApi2项目中遇到以下错误:

I am getting the below error in WebApi2 project:

无法加载文件或程序集'System.IdentityModel.Tokens.Jwt,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.找到的程序集的清单定义与程序集引用不匹配. (来自HRESULT的异常:0x80131040)

Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我已经安装了以下一组相关的NuGet软件包以及其他一些软件包:

I have these set of related NuGet packages installed, along with a bunch of others:

"Microsoft.IdentityModel.Protocol.Extensions"版本="1.0.2.206221351" targetFramework ="net45"

"Microsoft.IdentityModel.Protocol.Extensions" version="1.0.2.206221351" targetFramework="net45"

"Microsoft.Owin"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin" version="3.0.1" targetFramework="net45"

"Microsoft.Owin.Host.SystemWeb"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45"

"Microsoft.Owin.Security"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin.Security" version="3.0.1" targetFramework="net45"

"Microsoft.Owin.Security.ActiveDirectory"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin.Security.ActiveDirectory" version="3.0.1" targetFramework="net45"

"Microsoft.Owin.Security.Jwt"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin.Security.Jwt" version="3.0.1" targetFramework="net45"

"Microsoft.Owin.Security.OAuth"版本="3.0.1" targetFramework ="net45"

"Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45"

"System.IdentityModel.Tokens.Jwt" version ="4.0.2.206221351" targetFramework ="net45"

"System.IdentityModel.Tokens.Jwt" version="4.0.2.206221351" targetFramework="net45"

顺便说一句,我的web.config中也有下面的绑定重定向,但它仍然尝试加载4.0版本.

Btw, I have the below binding redirect in my web.config too but it still it tries to load the 4.0 version.

<dependentAssembly> <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.20622.1351" newVersion="4.0.20622.1351" /> </dependentAssembly>

在故障排除方面的任何帮助将不胜感激.

Any help in troubleshooting would be highly appreciated.

推荐答案

我遇到了完全相同的麻烦.

I ran into the exactly same troubles.

原因是,最新版本的System.IdentityModel.Tokens.Jwt和S​​ystem.IdentityModel.Tokens具有某些NuGet版本的混搭,并且与需要System.IdentityModel v.4.0.0.0的启动UseJwtBearerAuthentication方法不兼容.

The reason is, that the latest versions of System.IdentityModel.Tokens.Jwt and System.IdentityModel.Tokens has some NuGet versions mishmash and they're not compatible with startup UseJwtBearerAuthentication method which requires System.IdentityModel v. 4.0.0.0.

如果您使用的是nuget,您很容易感到困惑,因为:

If you're using nuget, you can be easily confused, because:

System.IdentityModel.Tokens在nuget中可用,与预发行版5.0.0.112(今天)一样

System.IdentityModel.Tokens is available in nuget just as pre-release 5.0.0.112 (nowdays)

nuget中的System.IdentityModel.Tokens.Jwt最新版本可作为稳定的预发行版本5.0.0.112或4.0.2.206221351.

System.IdentityModel.Tokens.Jwt latest version in nuget is available as pre-release version 5.0.0.112 OR 4.0.2.206221351 stable.

但是,当您在WebAPI中设置JWT身份验证时

BUT, when you set JWT authentication in WebAPI

app.UseJwtBearerAuthentication(new JwtOptions());

需要System.IdentityModel 4.0.0.0版本.

System.IdentityModel version 4.0.0.0 is required.

对我来说可行的解决方案是:

The working solution for me is:

1)卸载以前安装的System.IdentityModel.Tokens nuget程序包

1) uninstall previously installed System.IdentityModel.Tokens nuget package

Uninstall-Package System.IdentityModel.Tokens

2)卸载最新的System.IdentityModel.Tokens.Jwt nuget程序包

2) uninstall latest System.IdentityModel.Tokens.Jwt nuget package

Uninstall-Package System.IdentityModel.Tokens.Jwt

3)安装System.IdentityModel.Tokens.Jwt版本4.0.2.206221351(最新稳定版)

3) install System.IdentityModel.Tokens.Jwt version 4.0.2.206221351 (latest stable)

Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351

4)向.NET框架程序集System.IdentityModel添加引用(不是nuget!).右键单击项目->引用->添加引用->程序集->框架->选择System.IdentityModel 4.0.0.0

4) add reference (not nuget!) to .NET framework assembly System.IdentityModel. Right click on project -> References -> Add reference -> Assemblies -> Framework -> select System.IdentityModel 4.0.0.0

某些步骤可能会有所不同,具体取决于您已经安装/卸载了什么.

Some steps may differ depending on what have you already installed/uninstalled.

更多推荐

在WebAPI2项目中加载System.IdentityModel.Tokens.Jwt dll时出错

本文发布于:2023-11-13 00:24:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   项目   System   Jwt   dll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!