我有一个自定义 AuthorizationHandler
在我的 Asp.Net Core API 项目中,它根据在 Headers
中传递的 token 授权请求.Handler
工作正常,因为它在运行时调用但是我收到以下错误
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
我已在
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();
}
有什么建议?
最佳答案
该项目的默认属性是启用匿名身份验证和禁用 Windows 身份验证。
如果要选择 IIS 默认身份验证 作为您的 authenticationScheme ,您需要修改项目的属性以启用 Windows 身份验证并禁用匿名身份验证:
引用:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-2.2&tabs=visual-studio#iisiis-express
或者您可以添加其他身份验证方案(基于 cookie 的身份验证、JWT 不记名身份验证等),请参阅 Authorize with a specific scheme in ASP.NET Core更多细节 。
关于c# - Asp.Net Core 未找到 DefaultChallengeScheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971892/