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

Azure AD B2C自定义策略调用Graph API遇MethodNotAllowed错误求助

问题分析与解决方案

核心问题

你的自定义策略调用Graph API时出现错误,主要原因有两个:

  • 未指定正确的HTTP请求方法,默认POST与Graph API的GET接口不匹配
  • 请求参数的传递方式配置错误

具体修复步骤

1. 修正Graph API调用的TechnicalProfile配置

修改REST-GetProfile的Metadata和InputClaims,指定GET方法并调整参数传递方式:

<ClaimsProvider>
  <DisplayName>TEST CALL Graph API</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="REST-GetProfile">
      <DisplayName>Get user extended profile Azure Function web hook</DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <!-- 替换为实际的用户对象ID -->
        <Item Key="ServiceUrl">https://graph.microsoft.com/beta/users/{user-object-id}/memberOf</Item>
        <Item Key="AuthenticationType">Bearer</Item>
        <!-- 指定GET请求方法 -->
        <Item Key="HttpMethod">GET</Item>
        <!-- GET请求不需要传递参数到Body,设置为None -->
        <Item Key="SendClaimsIn">None</Item>
        <Item Key="UseClaimAsBearerToken">bearerToken</Item>
        <Item Key="AllowInsecureAuthInProduction">false</Item>
      </Metadata>
      <!-- 移除多余的InputClaims,bearerToken已经通过UseClaimAsBearerToken处理 -->
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

2. 针对/users/{userId}接口的额外说明

当调用https://graph.microsoft.com/beta/users/{userId}时,同样需要确保:

  • 添加<Item Key="HttpMethod">GET</Item>
  • 设置SendClaimsIn为None
  • 确认{userId}是用户的Azure AD B2C对象ID(不是用户主体名或邮箱)

3. 权限验证(补充排查)

虽然Postman测试正常,但仍需确认B2C租户中的应用注册:

  • 已添加Graph API的相应权限(如User.Read.All、GroupMember.Read.All)
  • 已完成管理员同意(client_credentials流程必须)

其他排查建议

  • 启用自定义策略的Application Insights日志,添加<Item Key="IncludeClaimResolvingInClaimsHandling">true</Item>到REST技术配置文件的Metadata中,以便更详细地查看请求/响应内容
  • 确认获取的bearerToken包含正确的权限范围(解码token查看roles声明)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 07:12:51