Error loading System.IdentityModel.Tokens.Jwt dll in WebAPI2 project

asp.net-web-api2jwtowin

I am getting the below error in WebApi2 project:

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)

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

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

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

"Microsoft.Owin.Host.SystemWeb" version="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.Jwt" version="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"

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.

Best Answer

I ran into the exactly same troubles.

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.

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

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

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

BUT, when you set JWT authentication in WebAPI

app.UseJwtBearerAuthentication(new JwtOptions());

System.IdentityModel version 4.0.0.0 is required.

The working solution for me is:

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

Uninstall-Package System.IdentityModel.Tokens

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

Uninstall-Package System.IdentityModel.Tokens.Jwt

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) 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.

Related Topic