You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置JwtBearer与MicrosoftIdentityWebApp并避免认证冲突?

如何让JWT Bearer和Microsoft Identity Web App两种认证路径不冲突?

我配置了两种认证方式,但它们始终互相覆盖,只有最后配置的那一种生效:

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(builder.Configuration);
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration)
        .EnableTokenAcquisitionToCallDownstreamApi(new string[] { "User.Read" })
        .AddInMemoryTokenCaches();

当前现象

  • 当AddMicrosoftIdentityWebAppAuthentication配置在下方时,登录页面正常显示且可用
  • 当JwtBearer相关配置在下方时,守护程序无需登录页面即可正常运行

已尝试操作

找到过与自身场景相似的问题,但未能充分理解并应用到实际配置中。

解决方案

无需修改原有配置代码,只需根据业务需求为对应控制器添加以下授权特性:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

内容的提问来源于stack exchange,提问作者StinkySocks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:10:00