API Management Inbound policy add header with subscription name

Eric Fitskie 96 Reputation points
2022-10-04T11:25:53.967+00:00

I'm using API Management as front end for our API's for our customer. On the back-end server i need to identify the customer for the request.

I want to forward the subscription name (not the display name) that correspondents with the ocp-apim-subscription-key.

I can't find how to convert the ocp-apim-subscription-key header to the corresponding subscription name?

Forwarding the subscription key is not a good candidate as this can be regenerated and i lose the matching between customer and subscription

Can someone help me how to do this? Or is there some better way to achieve this?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,960 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sujithreddy Komma 576 Reputation points Microsoft Employee
    2022-10-06T16:21:47.57+00:00

    Hi efitskie,

    Place the below Policy You will get the Name

    <set-variable name="ID" value="@(context.Subscription.Id)" />

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 24,111 Reputation points
    2022-10-06T17:11:19.03+00:00

    Hi @Eric Fitskie Thanks for reaching out. From the description I understand that you wanted to get the details of the users making request to your API's. To identify the customer, you can make use of below policy to get the username.

     <set-header name="user-name" exists-action="override">  
                <value>@(context.User.Id)</value>  
            </set-header>  
    

    To fetch the name of the subscription key

     <set-header name="name" exists-action="override">  
                <value>@(context.Subscription.Id)</value>  
            </set-header>  
    

    if you do not want to pass the subscription key header to your backend you can delete the header as below

    <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />  
    

    please refer the available policy expressions: https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions#ContextVariables

    let me know incase of further queries, I would be happy to assist you.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.