Well-Known Endpoints and Protecting Custom APIs with Microsoft Entra: A Developer's Guide

Introduction

In my journey as a developer working with Microsoft Entra (formerly Azure AD), I've encountered numerous scenarios where understanding well-known endpoints and API protection mechanisms became crucial for building secure applications. Today, I want to share my personal experience and knowledge about these fundamental concepts that every developer should understand when working with Microsoft's identity platform.

This guide will explore two critical aspects:

  1. Well-Known Endpoints in Microsoft Entra and their role in OpenID Connect discovery

  2. Protecting Custom APIs using Microsoft Entra authentication and authorization

What are Well-Known Endpoints?

Well-known endpoints are standardized URLs that provide discovery information for OpenID Connect providers. In the context of Microsoft Entra, these endpoints serve metadata about the identity provider's capabilities, supported flows, and configuration details.

The OpenID Connect Discovery Document

Microsoft Entra exposes a well-known configuration document at a predictable URL pattern:

https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration

Where {tenant} can be:

  • common - For multi-tenant applications

  • organizations - For work/school accounts only

  • consumers - For personal Microsoft accounts only

  • {tenant-id} - For single-tenant applications

What Information Does the Well-Known Endpoint Provide?

When you make a GET request to the well-known endpoint, you receive a JSON document containing:

Why Well-Known Endpoints Matter

From my experience, well-known endpoints are essential because they:

  1. Enable Dynamic Discovery: Applications can programmatically discover endpoint URLs instead of hardcoding them

  2. Support Multi-Tenant Scenarios: Different tenants may have slightly different configurations

  3. Facilitate Library Integration: Most authentication libraries automatically consume these endpoints

  4. Ensure Future Compatibility: Microsoft can update endpoint URLs without breaking applications

Protecting Custom APIs with Microsoft Entra

Now, let's dive into the practical aspect of protecting custom APIs using Microsoft Entra. I'll use a Python Flask API as an example to demonstrate the concepts.

Architecture Overview

Here's how the authentication flow works when protecting a custom API:

spinner

Step 1: Register Your API in Microsoft Entra

First, you need to register your custom API as an application in Microsoft Entra:

  1. Navigate to the Microsoft Entra admin center

  2. Go to App registrationsNew registration

  3. Configure your API application:

    • Name: my-custom-api

    • Account types: Choose based on your requirements

    • Redirect URI: Not needed for API-only applications

Step 2: Expose API Scopes

After registration, expose your API by defining scopes:

  1. Go to Expose an API

  2. Set the Application ID URI (e.g., api://my-custom-api)

  3. Add scopes such as:

    • api://my-custom-api/Data.Read

    • api://my-custom-api/Data.Write

Step 3: Create a Sample Python API

Here's a simple Flask API that demonstrates token validation:

Step 4: Configure API Management Protection

If you're using Azure API Management, you can configure JWT validation policies:

Step 5: Client Application Configuration

Here's how a client application would obtain and use tokens:

Best Practices from My Experience

Throughout my journey with Microsoft Entra and API protection, I've learned several important best practices:

1. Token Validation

  • Always validate the token signature using public keys from the JWKS endpoint

  • Verify the aud (audience) claim matches your API's identifier

  • Check the iss (issuer) claim to ensure it's from Microsoft Entra

  • Validate token expiration (exp) and not-before (nbf) claims

2. Scope-Based Authorization

  • Design granular scopes that represent specific permissions

  • Use descriptive scope names (e.g., Data.Read, Users.Write)

  • Implement scope checking in your API endpoints

3. Error Handling

  • Return appropriate HTTP status codes (401 for authentication, 403 for authorization)

  • Provide meaningful error messages without exposing sensitive information

  • Log security events for monitoring and auditing

4. Performance Considerations

  • Cache public keys with appropriate TTL

  • Use connection pooling for HTTP requests

  • Consider implementing token validation at the gateway level

5. Security Considerations

  • Use HTTPS for all communications

  • Implement rate limiting to prevent abuse

  • Regular security audits and token rotation

  • Monitor for suspicious activity

Troubleshooting Common Issues

Based on my experience, here are common issues and their solutions:

Issue 1: "Invalid Audience" Error

Problem: Token validation fails with audience mismatch Solution: Ensure the audience claim in the token matches your API's Application ID URI

Issue 2: "Invalid Signature" Error

Problem: JWT signature validation fails Solution: Verify you're using the correct public keys from the JWKS endpoint and handling key rotation

Issue 3: "Insufficient Scope" Error

Problem: User lacks required permissions Solution: Check that the client application has been granted the necessary scopes and admin consent if required

Conclusion

Understanding well-known endpoints and API protection with Microsoft Entra is crucial for building secure, modern applications. The well-known discovery document provides a standardized way to obtain configuration information, while proper API protection ensures that only authorized clients can access your resources.

Key takeaways from my journey:

  1. Leverage Well-Known Endpoints: Use the discovery document for dynamic configuration

  2. Implement Proper Token Validation: Always validate signatures, claims, and expiration

  3. Design Granular Scopes: Create meaningful permissions that align with your business logic

  4. Follow Security Best Practices: Use HTTPS, implement rate limiting, and monitor for threats

  5. Plan for Scale: Consider performance implications and caching strategies

By following these patterns and practices, you'll be well-equipped to implement secure API protection using Microsoft Entra in your applications. Remember, security is not a one-time implementation but an ongoing process that requires regular review and updates.

Additional Resources


This blog post reflects my personal experience and learnings while working with Microsoft Entra and API security. I hope this guide helps you in your own journey with identity and access management!

Last updated