Post

Integrate oAuth2 and OpenIDConnect with C#.Net API and Azure APIM

Introduction

This blog describes how to secure APIs with APIM and oAuth and OIDC.

Prerequisites

  1. Azure API Management: To protect Azure FunctionApp. public access to Azure function endpoint is restricted. Can be called only from APIM.
  2. Azure EntraID: To register APIM backend and client app, in this case Postman.
  3. Azure FunctionApp: Simple Httptrigger with Get endpoint that returns a simple message.
  4. Postman or Bash: To test

Architecture

Desktop View

  1. Postman is a client app in this case. It requests a JWT access token from Azure Entra ID using app registration client id and client secret. This is OIDC.
  2. Azure Entra returns JWT access token.
  3. Postman makes a request to APIM endpoint with Bearer token in the Authorization header.
  4. APIM validates the token with Azure Entra ID.
  5. APIM returns success payload or 401 unauthorization error.

In your scenario, you are using OIDC to authenticate the user/application and obtain a JWT token. You are then using OAuth 2.0 to authorize the client to access the protected resource, which is the API via APIM.

Different oAuth Grant types

Desktop View

Description of Grant types

Desktop View

High level steps required to configure oAuth in Azure Entra

Register an app: oauth-apim-backend in Azure Entra to represent the protected APIM resource.

Desktop View

Desktop View

Desktop View

Register another application client-app: oauth-client-app in Azure Entra which represent postman that wants to access the protected APIM resource.​

Desktop View Desktop View

Configure API management as shown below.

Desktop View

Testing oAuth, OIDC and APIM Desktop View

References:

This post is licensed under CC BY 4.0 by the author.