Asp.net-Core

包 Microsoft.AspNetCore.Authentication.JwtBearer 5.0.0 與 netcoreapp3.1 不兼容,但它的目標是 net 5.0

  • May 6, 2021

我正在從 .Net Core 3.1 遷移到 .NET 5.0。

我不斷收到此錯誤消息:

包 Microsoft.AspNetCore.Authentication.JwtBearer 5.0.0 與 netcoreapp3.1 (.NETCoreApp,Version=v3.1) / win-x86 不兼容。包 Microsoft.AspNetCore.Authentication.JwtBearer 5.0.0 支持:net5.0 (.NETCoreApp,Version=v5.0)

我明白它在說什麼,但我不知道它為什麼這麼說。

我的項目面向 .NET 5.0。

我的 csproj 文件如下所示:

<Project Sdk="Microsoft.NET.Sdk.Web">

 <PropertyGroup>
   <TargetFramework>net5.0</TargetFramework>
   <UserSecretsId>5e2f8086-7a94-402a-bd2a-4160e9236c8f</UserSecretsId>
 </PropertyGroup>


<ItemGroup>
   <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
   <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" />
   <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
   <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
   <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.0" />
   <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
 </ItemGroup>

</Project>

我不確定為什麼它告訴我關於 3.1。

我手動刪除了 bin 和 obj 文件夾,嘗試更新所有 nuget 包,並清理/重建項目,但每次都遇到相同的問題。v

我在 Visual Studio 16.8.2 上。

編輯

我應該指出,錯誤不是編譯器錯誤!如果我在 Visual Studio 中進行調試,網站會正常執行。此錯誤消息僅在我嘗試發佈時出現

如果您尚未移至 5,則可以通過點擊 nuget 中的版本下拉菜單來安裝 microsoft.aspnetcore.authentication.jwtbearer 版本 3.1.x。

您可以檢查以下步驟來解決您的問題。

  1. 在此處下載 NET 5.0 SDK 。
  2. 您需要Visual Studio 16.8或更高版本在 Windows 上使用 .NET 5.0。請確認您的 VS 版本。
  3. package:<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />已過時,請刪除項目文件中的引用。
  4. 清潔和重建。

引用自:https://stackoverflow.com/questions/65097569