Share via

Support Subscription-key based authentication or OAUTH2 for APIM endpoint

ujjwalDev 46 Reputation points
2022-01-19T22:57:20.853+00:00

Hi,

I have an APIM hosted api endpoint. I have two different clients. One client should access this api with just a subscription-key. Other client should access this with OUTH2 JWT. How can I achieve this on the policy.

If I disable the subscription-key required flag for the policy, how to achieve the subscription-key based access.

Thanks,
Ujjwal

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.


1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,661 Reputation points Microsoft Employee Moderator
    2022-01-28T08:45:13.267+00:00

    @ujjwalDev Even if a subscription isn't required, when passed it is still parsed. So, you could use the choose statement to decide based on the header set like below

       <choose>  
           <when condition="@(context.Subscription?.Name != null)">  
               <!-- Subscription ID was present -->  
           </when>  
           <when condition="@(!String.IsNullOrWhiteSpace(context.Request.Headers.GetValueOrDefault("Authorization", "")))">  
               <!-- Authorization Header was present -->  
           </when>  
           <otherwise>  
               <!-- Neither was present -->  
           </otherwise>  
       </choose>  
    

    Was this answer helpful?

    0 comments No comments

Your answer

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