Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Entra authentication for Azure Static Web Apps

This guide walks you through configuring a custom identity provider for a Static Web App to restrict access to your organization's users only.

1. Create an app registration

Go to App registrations page in Entra. Click New Registration. Check that supported account types is set to Accounts in this organizational directory only. Select a name for your registration and click Register.

2. Configure platforms

2.1 Redirect URIs

Navigate to your App Registration and go to Authentication blade. Under Platform configurations, click Add a platform.

Select Web platform. Set a redirect URI to https://yourstaticwebappurl.azurestaticapps.net/.auth/login/aad/callback. If you want to test your authentication locally with Static Web Apps CLI, add another redirect URI to https://localhost:4280/.auth/login/aad/callback. Then click Configure.

Redirects configuration screenshot

2.2 Implicit grant and hybrid flows

The authorization endpoint needs to be able to issue access tokens for the app. This guide assumes using the default Azure authentication flow (no MSAL), only access tokens need to be enabled. Select them and click Save.

Tokens configuration screenshot

3. API permissions

Go to API permissions view in your app registration. Click Add a permission -> Microsoft Graph -> Delegated permissions -> Search for User.Read and select it. Click Add permissions. The result should look like this:

Permissions configuration screenshot

4. Create a client secret

Go to Certificates & secrets in your app registration. Select New client secret. Set a description and expiry, and click Add. Copy the secret value for use in the next step.

5. Configure the Static Web App

5.1 Set environment variables

Navigate to your Static Web App -> Environment Variables. Set two client variables:

  • AZURE_CLIENT_ID (the client id of your app registration)
  • AZURE_CLIENT_SECRET (value of the secret created in the previous step)

Click Apply.

5.2 staticwebapp.config.json

In your staticwebapp.config.json, set the following properties:

"auth": {
            "identityProviders": {
                "azureActiveDirectory": {
                    "registration": {
                        "openIdIssuer": "https://login.microsoftonline.com/{your_tenant_id}/v2.0",
                        "clientIdSettingName": "AZURE_CLIENT_ID",
                        "clientSecretSettingName": "AZURE_CLIENT_SECRET"
                    }
                }
            }
        },
"routes": [
		{
			"route": "/logout",
			"statusCode": 302,
			"redirect": "/.auth/logout"
		},
		{
			"route": "/*",
			"allowedRoles": ["authenticated"]
		}
	],
"responseOverrides": {
		"401": {
			"statusCode": 302,
			"redirect": "/.auth/login/aad"
		}
	}  

Insert your tenant id. Set routes and response overrides per your use case, these are just working defaults.

Run the configuration either locally or in Azure and verify you're able to login.

Links

About

Cheatsheet and helpers for securing your Static Web Apps with Entra

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors