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

后端 未结 2 1230
小蘑菇
小蘑菇 2021-02-07 10:54

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=31bf

2条回答
  •  梦如初夏
    2021-02-07 11:06

    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.

提交回复
热议问题