首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AzureAd JwtBearer未指定authenticationScheme,并且未找到DefaultChallengeScheme

AzureAd JwtBearer未指定authenticationScheme,并且未找到DefaultChallengeScheme
EN

Stack Overflow用户
提问于 2021-08-16 15:18:56
回答 1查看 341关注 0票数 1

我正在尝试修改我的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).

EN

回答 1

Stack Overflow用户

发布于 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包

根据错误,可以通过以下方式尝试添加默认方案:

在配置服务方法下的启动类中

代码语言:javascript
运行
复制
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
}

代码语言:javascript
运行
复制
 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(); 

  } 
           };

              });
//
//
}

(或)

代码语言:javascript
运行
复制
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

参考:

  1. Azure-Samples
  2. microsoft-identity-web
  3. SO ref-signalR
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68805350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档