App Identity APIM Policy

Upasana Ghosh 351 Reputation points
2022-10-27T12:43:24.303+00:00

Need Azure APIM policy which satisfies the below conditions :
(1) A specific HTTP Header (ConsumerId) is used to carry the initial consumer identity
(2) If the header doesn't exist in the incoming request, the gateway sets it in the request towards the provider to the value of the "sub" claim of the received Access Token
(3) If the header exists, the gateway simply forwards it in the request to the provider

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,455 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,020 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2022-10-27T14:46:54.373+00:00

    @Upasana Ghosh Thank you for reaching out to Microsoft Q&A. Based on my understanding, you want to add custom header ConsumerId with value of sub claim from the access token only if header does not exist.

    You can use set-header policy with skip action so that it doesn't replace the existing header value and refer docs as below.
    254807-image.png

    For decoding authorization token, use .AsJwt() method in Context variable and try the below snippet (based on answer shared by our PG in SO)
    254730-image.png

    Policy snippet:

        <set-header name="ConsumerId" exists-action="skip">  
                    <value>@{  
                        var jwt = context.Request.Headers.GetValueOrDefault("Authorization").AsJwt();  
                        return jwt?.Claims.GetValueOrDefault("sub") ?? "unknown";  
                    }</value>  
                </set-header>  
    

    I hope this answers your question and feel free to add a comment if you have any other questions. We would be happy to assist you. Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.