WalkThrough-AzureActiveDirectoryMultiTenant
WalkThrough-AzureActiveDirectoryMultiTenant copied to clipboard
Azure Active Directory multi-tenant walkthrough
Walkthrough - Azure Active Directory multi-tenant
A step by step guide to creating an ASP.NET hosted Blazor WASM client that uses Azure Active Directory to authenticate.
NOTE: You can read my blog covering this Here - it is almost identical to this document, except it has some additional comments explaining some of the steps.
Why multi-tenant?
Allows absolutely anyone to use your app as long as they have authenticated using Azure Active Directory. The user doesn't need to be in your directory, just some directory.
Note that this doesn't restrict who can access your application, nor what they can do once they've signed in. It just ensures you know the person using your application has gone through the necessary sign-in process on Azure Active Directory.
Creating your Active Directory
Enable Active Directory as a resource provider
- Sign into https://portal.azure.com
- Select your subscription
- In the menu on the left hand side (Left menu) select
Resource providers - Click
Microsoft.Azure.ActiveDirectory - If its status is not
Registered, clickRegisterat the top of the page
Set up Azure Active Directory multi-tenant
- From the Azure home page click
Create a resource - Search for
Azure Active Directory - Find the
Azure Active Directoryicon (NOT B2C) - Click the
Createlink at the bottom of the item - Click
Azure Active Directoryin the popup menu - For
Tenant typeselectAzure Active Directory(NOT B2C) - Click
Next - Enter an
Organization nameandInitial domain name(e.g.MyOrgandMyOrgInitialDomain) - Select your
Location - Click
Review + create - Click
Create
Switch to your new directory
- Click your user account icon at the top-right of the page
- Click
Switch directoryin the popup menu - Refresh your browser if your new tenant is not listed
- Click
Switchnext to your new directory in the list
Registering your web application with Azure AAD
Register your application
- Click the three horizontal lines (burger menu) at the top-left of the page
- Select
Azure Active Directoryfrom the popup menu - In the left-menu, click
App registrations - Click
New registrationat the top of the page - Give it a name (e.g. "MyApp")
- For
Supported account typesselectAccounts in any organizational directory (Any Azure AD directory - Multitenant) - Under
Redirect URIselectSingle-page application (SPA)for the platform - For the URI enter
https://localhost:6510/authentication/login-callback - In the left menu, click
Overview - Make a note of the value of
Application (client) ID
Callback URI of your website
- In the left menu, click
Authentication - Under
Single-page applicationclickAdd URI - Enter the login-callback URI of your app (e.g.
https://ibm.com/authentication/login-callback) - Click
Save
Expose an API for your application
- Click the burger menu at the top-left of the page
- Select
Azure Active Directory - In the left menu, click
App registrations - Click your application in the list
- In the left menu, click
Expose an API - At the top of the page, find
Application ID URIand clickSet - It will default to
api://{Application (client) ID}, this ID is acceptable (NOTE:{}denotes the value, e.g.d581756b-53b0-4957-820a-d6aa43fd69de) - Click
Save
Add a scope to your API
- Click
Add a scope - For
Scope nameenter a name you wish to use to identify the scope (e.g.access_as_user) - For
Who can consentselectAdmins and users - For
Admin consent display name enterRead user's profile information` - For
Admin consent descriptionenterAllows the application to read the user's profile information - For
User consent display nameenterRead your profile information - For
User consent descriptionenterAllows the application to read your profile information - Ensure
Enabledis selected - Click
Add scope
Add permissions for your application to use the access_as_user scope
- In left menu, click
API permissions - Click
Add a permission - Click the
[My APIs]tab at the top of the popup menu - Click your application in the list of items
- In the
Permissionssection ensureaccess_as_useris checked - Click `Add permissions
Add permissions for your application to access additional user information
- In the same
API permissionspage, clickAdd a permission - Select the
Microsoft Graphitem at the top of the popup menu - Click the
Delegated permissionsitem in the popup menu - Ensure
emailandopenidare both checked - Click 'Add permissions`
Finishing adding permissions
When you've finished the above steps, click the Grant admin consent for {Your organisation name} and then click Yes`. Remember this step if you add any additional permissions
in future.
Creating your web application
Create the solution
- Create a new ASP.NET hosted Blazor WASM application in Visual Studio
- IMPORTANT: For
Authentication typeselectMicrosoft identity platform - The
Required componentswizard will appear and try to connect to your AAD registered app. This will also add items to AAD that we don't want (another app registration) - Click
Cancel
Match our sign-in callback URL to the registered application's
Edit Properties/launchSettings.json in both the client and
server app, and set the applicationUrl to https://localhost:6510
Set client application configuration for AAD
- Edit
wwwroot/appsettings.json - Replace the contents with the following
{
"AzureAd": {
"ClientId": "{Application (client) ID}",
"Authority": "https://login.microsoftonline.com/organizations"
},
"ServerApi": {
"Scopes": "api://{Application (client) ID}/access_as_user"
}
}
- Replace
{Application (client) ID}with the GUID you noted earlier
Set up client authentication to read from our configuration
- Edit
Program.cs - Change the
AddMsalAuthenticationsection so scopes are read from the config file
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
string? scopes =
builder.Configuration!.GetSection("ServerApi")["Scopes"]
?? throw new InvalidOperationException("ServerApi::Scopes is missing from appsettings.json");
options.ProviderOptions.DefaultAccessTokenScopes.Add(scopes);
// Uncomment the next line if you have problems with a pop-up sign-in window
// options.ProviderOptions.LoginMode = "redirect";
});
Set server application configuration for AAD
- Edit
appsettings.json - Replace the
AzureAdsection with the following
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "organizations",
"ClientId": "{Application (client) ID}",
"Scopes": "api://{Application (client) ID}/access_as_user"
}
- Replace
{Application (client) ID}with the GUID you noted earlier
Update the server to use WebApiAuthentication and add Authorization
- Edit
Program.cs - Replace
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
with
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
builder.Services.AddAuthorization();
Remove server Views support (optional)
- Edit
Program.cs - Replace
builder.Services.AddControllersWithViews();
with
builder.Services.AddControllers();
Remove server Razor Pages support (optional)
- Edit
Program.cs - Remove
builder.Services.AddRazorPages(); - Remove
app.UseRazorPages();
Remove SCOPE requirement from server controller (workaround)
This step is only required until I work out why the user's scopes are not being passed to the server.
- Edit
WeatherForecastController.cs - Comment out the line with the
[RequiredScope]attribute on it