升级到 ASP.NET Core 3 后使用 OAuth 进行身份验证时出现 MissingMethodException

MissingMethodException when authenticating with OAuth after upgrading to ASP.NET Core 3

我正在尝试将我的 ASP.NET 核心应用程序从 2.2 升级到 3.0。它使用 OAuth 向第三方进行身份验证 API。它在本地工作正常,但是当我 运行 它在我的服务器上时,会生成以下异常:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request. System.Exception: An error was encountered while handling the remote login.
---> System.MissingMethodException: Method not found: 'Newtonsoft.Json.Linq.JObject > Microsoft.AspNetCore.Authentication.OAuth.OAuthTokenResponse.get_Response()'.

我不确定如何对此进行调试,或者在此提供哪些其他信息会有用。有谁知道如何找出问题所在?

身份验证发生在 class 库中,它仍在引用 Microsoft.AspNetCore.Authentication.OAuthMicrosoft.AspNetCore.Authentication.Cookies。版本 2.2。我得到的错误是因为在 .NET Core 3 中,OAuthTokenResponse.Response 被更改为使用 System.Text.Json 而不是 Newtonsoft。我想在我的开发机器上,它加载了旧版本 Microsoft.AspNetCore.Authentication.OAuth,而在我的生产环境中它加载了新版本。

遗憾的是,Microsoft.AspNetCore.Authentication.OAuth 的最新版本无法作为单独的软件包提供(至少在撰写本文时)。为了使用它,我必须对我的 class 库进行以下更改:

  • 将其更改为目标 netcoreapp3.0 而不是 netstandard2.0
  • 删除对 Microsoft.AspNetCore.Authentication.OAuthMicrosoft.AspNetCore.Authentication.Cookies
  • 的引用
  • Microsoft.AspNetCore.App 中添加一个 FrameworkReference

进行这些更改后,身份验证再次在生产中为我工作。

在我的例子中,我需要 google 和微软服务帐户(在 dot net core 3.0 中):

  1. 删除(或卸载)Microsoft.AspNetCore.Authentication.OAuth Nuget 包
  2. 添加(或安装)OAuth2 Nuget 包
  3. 添加Microsoft.AspNetCore.Authentication.Google Nuget 包
  4. 添加Microsoft.AspNetCore.Authentication.MicrosoftAccount Nuget 包裹

现在可以使用了。