在 WebAPI2 项目中加载 System.IdentityModel.Tokens.Jwt dll 时出错 [英] Error loading System.IdentityModel.Tokens.Jwt dll in WebAPI2 project

查看:27
本文介绍了在 WebAPI2 项目中加载 System.IdentityModel.Tokens.Jwt dll 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting the below error in WebApi2 project:

无法加载文件或程序集System.IdentityModel.Tokens.Jwt, Version=4.0.0.0, Culture=neutral, 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" version="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" version="3.0.1" targetFramework="net45"

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

"Microsoft.Owin.Security.Jwt" version="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 和 System.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 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆