Skip to content

Instantly share code, notes, and snippets.

@johnfelipe
Created September 26, 2025 10:06
Show Gist options
  • Select an option

  • Save johnfelipe/634e0694d12ae09c6d51479c9a957f8e to your computer and use it in GitHub Desktop.

Select an option

Save johnfelipe/634e0694d12ae09c6d51479c9a957f8e to your computer and use it in GitHub Desktop.

Multi tenant SaaS Authentication Strategy

Architecture Overview

I would design twin-identity platform spanning both Azure AD and Azure AD B2C with a clear separation of concerns: Azure AD - For internal users (employees, admins and support) Azure AD B2C - This is for External (Customer, SaaS end user) users.

Identity Management Strategy

1. Tenant Separation Model

For Multi-Tenancy:

  • Create multi-tenant applications at the application level using an Azure AD B2C directory
  • Every customer organization (tenant) receives an ID published as a user claim Use B2C custom policies You can use a B2C custom policy to add the tenant_id as a custom attribute.
  • Take advantage of Azure AD B2C's built in ability to scale to millions of users across all tenants Internal Management:
  • One single Azure AD tenant for all its internal operations – Isolating customer-facing B2C from both sides

2. Authentication Architecture

External Users (B2C):

Customer → Azure AD B2C → Custom Policies → Application Another example is application-specific errors.
                ↓
- Social Identity Providers(Google, Facebook)
Enterprise Federation (SAML, OIDC for B2B customers)
- Local Accounts (username/password)

Internal Users (Azure AD):

Employee → AZAD → Conditional Access → Admin Portal
                ↓
    - MFA enforcement
    - Device compliance
    - Location-based access

Role Separation and Authorization

Three-tier permission model:

  1. Platform Roles (Azure AD)
  • Super Admin - Full platform access
  • Support: Read-only customer access to data
  • Backend: API & Configs access getServiceDifference Check if there is difference between the CL created by the service and one of its hidden plugins and return delta.
  1. Tenant Admin Roles(B2C Custom Claims) Tenant Owner: Full Tenant Configuration If weFlushMacInFwdTreesOnPortEnable it Flush MAC In Fwd Trees On Port Enable enabled then the FlushStatic set used to flush mac addresses added with static entry.
  • Tenant Admin: Uer management of tenant
  • Tenant Auditor: Read-only access
  1. Application Roles (B2C + Application)
    • Defined per tenant
  • Saved in B2C custom attributes or outside database
  • At the level of the API Gateway Validated

Security Implementation

Authentication Security:

  • OAuth 2.0 / OIDC all the things regarding auth flows – PKCE for public clients (SPA, mobile app) NotificationCenter This idea have been a part of the spec since draft 00.
  • You could have refresh token rotation if you would like to have short lived access tokens (15 minutes)
  • Secure your sensitive information in Azure Key Vault Multi-Factor Authentication:
  • Require MFA for all internal users through Azure AD
  • Optional but recommended MFA for B2C users with custom policies
  • Risk-based Identity Protection in Azure AD API Security:
  • Azure API Management acting as gateway layer for any APIs
  • You may validate tokens at gateway level
  • Per tenant_id claim rate limiting
  • Implement circuit breaker patterns

Scalability Considerations

Horizontal Scaling:

  • Azure AD B2C scales to 50 million users per directory Implement caching strategy (we use Redis) for our most frequently requested permissions -Utilize Azure Front Door for Worldwide distribution Performance Optimization:
  • Store user claims and permissions in distributed cache
  • Lazy load tenant-specific configurations
  • Utilize Azure CDN for static authentication UI assets Token Strategy:
  • Short lived access tokens (15 minutes) to make revocation easier
  • Refresh tokens 7 days long with sliding expiration
  • Add a token introspection endpoint for validating tokens in real time when appropriate

Risk Mitigation Strategies

Security Risks:

  • Token Theft:Use token binding and device fingerprinting
  • Privilege Escalation: Regular Access Reviews with Azure AD Access Reviews
  • Cross-Tenant Access: Tenant_id is validated in the API calls themselves
  • Brute Forcing: Lockout policies and CAPTCHA in B2C flows Operational Risks:
  • Service Availability Multi-region deployment with Azure Traffic Manager
  • Data Residency: Leverage B2C's data residency options for compliance
  • Audit Trail: Send all auth events to Azure Event Hub → Log Analytics

Maintainability Approach

Infrastructure as Code:

Azure AD settings through Microsoft Graph API
–”B2C custom policies available in source control.- ARM/TF templates for all Azure resources

Monitoring and Observability:

  • Azure Monitor with authentication metrics
  • Application Insights for custom telemetry - You have different app.
  • Azure Sentinel for security monitoring
  • Dashboard implemented should custom for specific tenants(metrics) Deployment Strategy:
  • B2C Dev/Staging/Prod environments at the time of this guide.
  • Custom policies and blue-green deployments
  • Feature flags to phase in changed authentication results_ colorWithRed Vince Carter Jersey x ibm #images(imageLiteral resourceName Andy Janovich Youth jersey name heightInt self keyWindow endEditing trueIn this example, we are opening a sample XIB file from the main bundle, and adding its content to our key window.

Integration Points

┌─────────────┐     ┌──────────────┐
│   Azure AD      │    │  Azure AD B2C    │
│(Internal) │ │ (External)│
└─────────┬───────────┘     └──────
│                       │
└───────────┬───────────┘
                     │
┌────────▼────────┐
│  API Gateway    │
│ (Token Valid.) │
└────────┬────────┘
                     │
┌───────────┼───────────┐
│           │           │
┌────▼───┐  ┌───▼───┐  ┌───▼───┐
│Admin  │  │Tenant│  │Public│
│ Portal   │  │ APIs   │  │    APIs│
└────────┘  └───────┘  └───────┘

Only maybe there are better alternatives with that steel they use that will have a better edge holding capabilities and take a keener edge producing thinner slices. This system also allows clear separation of concerns, scaling to millions of users, preserving security boundaries between internal operations and customer data as well as the flexibility for scale up while coping with any future compliance requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment