首页 > 解决方案 > 升级“DependencyInjection”后的 C# 身份验证问题

问题描述

我正在为项目开发一个同时使用 .NET Core 2.1 和 .NET Core 3.1 的解决方案。我最近不得不Microsoft.Extensions.DependencyInjection从 2.2.0 升级到 3.0.0 版本。但是,现在每当我尝试访问端点时都会出现运行时错误:

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

现在,当我在网上搜索时,除了 Microsoft 文档和 GitHub 帖子中提到的定义之外,我找不到太多东西,它分别简单地说应该provide the AuthenticationHandler和它是responsible for returning the correct handler instance for a given scheme and request.

它显然中断了来自 AuthenticationMiddleware 类的 Invoke 调用,但我相信我在某处读到你不应该轻易创建自己的身份验证中间件,并且该类应该“开箱即用”,所以我不太确定如何着手解决这个问题。

恐怕我对身份验证不是很有经验,所以我不确定你需要什么代码来发现问题。如果有什么重要的遗漏,请发表评论,我会添加它。非常感谢任何和所有帮助。

标签: c#authentication.net-core

解决方案


我正在为项目开发一个同时使用 .NET Core 2.1 和 .NET Core 3.1 的解决方案。我最近不得不将 Microsoft.Extensions.DependencyInjection 从 2.2.0 升级到 3.0.0 版本。但是,现在每当我尝试访问端点时都会出现运行时错误:

System.InvalidOperationException:没有为“Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider”类型注册服务。在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 在 Microsoft .AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task) 现在,当我在线搜索时,我可以'

它显然中断了来自 AuthenticationMiddleware 类的 Invoke 调用,但我相信我在某处读到你不应该轻易创建自己的身份验证中间件,并且该类应该“开箱即用”,所以我不太确定如何着手解决这个问题。

恐怕我对身份验证不是很有经验,所以我不确定你需要什么代码来发现问题。如果有什么重要的遗漏,请发表评论,我会添加它。非常感谢任何和所有帮助。


推荐阅读