【发布时间】:2021-12-26 16:11:07
【问题描述】:
使用 Apim,我正在尝试调用需要 OAuth2 验证的后端 Api。这个问题或多或少类似于:Azure API Management: Oauth2 with backend API 但是这里没有好的答案……
我一直在阅读有关策略和缓存的大量内容。 但似乎无法正确设置。我希望能够调用 apim,然后 apim 调用后端 api 来获取令牌,并使用该令牌调用 Api 来获取一些输出数据。 我还发现了一个我必须在后端设置一些策略的地方.. 谁能帮我制定政策?
我的政策是这样的:
<policies>
<inbound>
<base />
<set-variable name="originBearer" value="@(context.Request.Headers.GetValueOrDefault("Authorization", "empty_token").Split(' ')[0].ToString())" />
<send-request ignore-error="true" timeout="20" response-variable-name="bearerToken" mode="new">
<set-url>{{lookupAccessTokenUrl}}</set-url>
<set-method>GET</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/x-www-form-urlencoded</value>
</set-header>
<set-body>@{
return "client_id={{HLR-app-client-id}}&scope={{HLR-scope}}&client_secret={{HLR-secret}}&assertion="+(string)context.Variables["originBearer"]+"&grant_type=urn:ietf:params:oauth:grant-type:client_credentials&requested_token_use=on_behalf_of";
}</set-body>
</send-request>
<set-variable name="requestResponseToken" value="@((String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])" />
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + (string)context.Variables["requestResponseToken"])</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
【问题讨论】:
标签: azure oauth-2.0 policies apim