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

使用MicrosoftIdentityWebApp实现Azure AD B2C刷新令牌的疑问及问题

Azure AD B2C认证与令牌刷新相关问题

我们目前使用AddMicrosoftIdentityWebApp实现Azure AD B2C认证,代码如下:

services.AddAuthentication()
.AddCookie(FRONTEND_COOKIE_SCHEME) // 因后端认证协议独立而使用的特殊Cookie
.AddMicrosoftIdentityWebApp(options =>
{  
    // 配置信息已省略以精简内容
    options.SignInScheme = FRONTEND_COOKIE_SCHEME;
    options.SignOutScheme = FRONTEND_COOKIE_SCHEME;
    options.UseTokenLifetime = true;
    options.SaveTokens = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = ClaimTypes.NameIdentifier
    };
})
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();

我们计划实现刷新令牌流程,已确认.well-known端点显示该功能已启用:

"response_types_supported": [
        "code",
        "code id_token",
        "code token",
        "code id_token token",
        "id_token",
        "id_token token",
        "token",
        "token id_token"
    ],

同时确认流程第一步已触发:访问认证端点时,会重定向到oauth2/v2.0/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=openid%20profile%20offline_access&code_challenge={codeChallenge}&code_challenge_method=S256&response_mode=form_post&nonce={nonce}&state={state}&ui_locales=en。但在OnTokenValidated步骤的响应中未发现code,推测其在OnAuthorizationCodeReceived步骤被消费,打算在此处实现刷新令牌获取流程,但相关参考文档较少,故咨询:作为MicrosoftIdentityWebApp的使用者,是否需要显式实现刷新令牌流程,还是框架会原生处理,仅需启用对应配置?


更新1

经研究发现MicrosoftIdentityWebApp确实会处理令牌交换流程,但遇到三个新问题:

  • 为在默认流程中获取access_token,需将response_type和scope从response_type=code&scope=openid profile offline_access修改为response_type=code id_token token&scope=openid profile offline_access {clientId},但在AddMicrosoftIdentityWebApp配置中设置options.ResponseType和options.Scope未生效,请求仍使用原配置,需通过拦截OnRedirectToIdentityProvider动作修改才生效,询问原因。
  • 已将token添加到ResponseType,但未按预期获取到refresh_token。在OnAuthorizationCodeReceived步骤中查看令牌请求,发现虽之前包含offline_access,但请求的scope属性未设置,怀疑此为问题原因。
  • 尝试从ResponseType中移除id_token,仅保留code token,此时未获取到access_token,但仍收到id_token。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 09:22:43