Thursday, August 18, 2022

Get OAuth2 Token to WebAPI using Azure AD

How to call API endpoint protected by Authentication and Authorization and get 200 OK instead of 401 Unauthorized?
  • Created minimal API in Visual Studio 2022 with auth backed by Azure AD
  • This registers API app with Azure AD and writes to app settings file: Azure client id, AD domain, Tenant ID, scope
  • Created API request in Postman with https://localhost:1234/endpoint URL
  • Getting 401 Unauthorized
  • Add Auth2.0 configuration
  • Requires registering Client App in Azure AD

Gotchas

Client ID has to be registered in Azure AD App registrations and added to the list of clients for API

Client Secret has to be created in Azure AD

The scope entered as data.read in Azure has to be entered with fully qualified form in Postman Scope: https://GuznerConsulting.onMicrosoft.com/WebApi/data.read