我正在尝试修改我的AzureAd身份验证以使用SignalR。因此,我更改了身份验证,以便可以添加更多选项。
在我使用services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd");
之前,它运行得很好。
根据此AzureAd;`
但是,当我点击我的API时,我得到了一个System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
。
发布于 2021-08-27 12:02:34
错误:System.InvalidOperationException:未指定authenticationScheme,并且未找到DefaultChallengeScheme。可以使用AddAuthentication(string defaultScheme)或AddAuthentication(操作configureOptions)设置默认方案。
使用Microsoft identity platform(refer)登录用户时,添加Microsoft.Identity.Web和Microsoft.Identity.Web.UI NuGet包
根据错误,可以通过以下方式尝试添加默认方案:
在配置服务方法下的启动类中
using Microsoft.Identity.Web;
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(
options => { Configuration.Bind("AzureAd", options);
//options.Events = new JwtBearerEvents
//
//custom code
}
或
public void ConfigureServices(IServiceCollection services)
{
services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme,
options => {
options.Events.OnAuthenticationFailed = async failedContext => {
if (failedContext.Exception.HResult == -2147024891)
{
failedContext.Response.StatusCode = 403;
await failedContext.Response.CompleteAsync();
}
};
});
//
//
}
(或)
public void ConfigureServices(IServiceCollection services)
{
//
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddMicrosoftIdentityWebApi(
options =>
{
//
Configuration.Bind("AzureAd", options);
//options.Events = new JwtBearerEvents
//
//custom code
}
}
还要确保在app.UseAuthorization()之上的Configure()方法中有这一行cod app.UseAuthentication();。
另请检查this
参考:
https://stackoverflow.com/questions/68805350
复制相似问题