首页 > 解决方案 > Asp.Net Core 未找到 DefaultChallengeScheme

问题描述

AuthorizationHandler我在我的 Asp.Net Core API 项目中有一个自定义,它根据在Headers.

工作正常,Handler因为它在运行时调用但是我收到以下错误

InvalidOperationException:未指定 authenticationScheme,也未找到 DefaultChallengeScheme。

我已将我的设置注册Startup.cs如下:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    services.AddSingleton<IAuthorizationHandler, TokenAuthorizationHandler>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseAuthentication();
    app.UseMvc();
}

有什么建议么?

标签: c#authenticationasp.net-core

解决方案


该项目的默认属性是启用匿名身份验证和禁用 Windows 身份验证。

如果您想选择IIS 默认身份验证作为您的 authenticationScheme ,您需要修改项目的属性以启用 Windows 身份验证并禁用匿名身份验证:

  • 右键单击解决方案资源管理器中的项目并选择Properties

  • 选择调试选项卡。

  • 清除Enable Anonymous Authentication的复选框。

  • 选中启用 Windows 身份验证复选框。

  • 保存并关闭属性页。

参考:https ://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-2.2&tabs=visual-studio#iisiis-express

或者您可以添加其他 authenticationSchemes(基于 cookie 的身份验证、JWT 不记名身份验证等),有关详细信息,请参阅使用 ASP.NET Core 中的特定方案进行授权。


推荐阅读