在 azure api 管理服务中为单个 api 添加多个 b2b 和 b2c 端点的 validate-jwt 策略是否有效?
# b2c endpoint
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/tfp/contoso.onmicrosoft.com/b2c_1_signin/v2.0/.well-known/openid-configuration" />
<audiences>
...
</validate-jwt>
# b2b endpoint
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/contoso.onmicrosoft.com/.well-known/openid-configuration" />
<audiences>
...
</validate-jwt>
是的,您可以在单个 API 中添加多个 validate-jwt 策略,但在 b2c 和 b2b 等某些条件下,您需要将每个 validate-jwt 保留在
<choose>
块中。
如果您在共享时直接添加它们,那么它会将令牌与两个端点进行比较,并会给您 401 错误,因为其中之一永远不会匹配。
所以,建议将其保留在
<choose>
块中。
您可以在
<when>
中给出任何您想要的条件,以在 b2b 和 b2c 之间进行过滤。因此,传入请求标头中的令牌将与正确的端点进行比较。
<policies>
<inbound>
<base />
<choose>
<!-- B2C Endpoint -->
<when condition="@(context.Request.OriginalUrl.Contains('/b2c'))">
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/tfp/contoso.onmicrosoft.com/b2c_1_signin/v2.0/.well-known/openid-configuration" />
<audiences>
<audience>Your_B2C_Client_ID</audience>
</audiences>
</validate-jwt>
</when>
<!-- B2B Endpoint -->
<when condition="@(context.Request.OriginalUrl.Contains('/b2b'))">
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/contoso.onmicrosoft.com/.well-known/openid-configuration" />
<audiences>
<audience>Your_B2B_Client_ID</audience>
</audiences>
</validate-jwt>
</when>
</choose>
</inbound>
</policies>