Using Microsoft.Identity.Web templates to connect to Azure AD B2C

Rory Braybrook
The new control plane
3 min readSep 30, 2020

First we create the app registration in the B2C portal.

Note you must select the third option.

You should get:

Note that the “Supported account types” must be “All users”.

The client_id and the tenant_id are use in the command line command further down.

There’s a neat feature you can use to complete the registration.

Use the “Integration assistant”.

Select “Web app” and then “Evaluate my app registration”.

You’ll see you need to complete two things.

Click the three dots and then “Go to Page”.

Click “Add a platform”.

Select “Web”.

Then configure the redirect URI and select the token types you want.

You could also do this direct from the “Authentication” tab on the LHS.

Read this post to give you some background on the templates.

As per that post, we are going to put the B2C parameters in the command line:

dotnet new mvc2 
--framework net5.0
--auth IndividualB2C
--domain tenant.onmicrosoft.com
--tenant-id 65f...316
--client-id 40a...a8c
--susi-policy-id B2C_1_SUSI_V2

where “B2C_1_SUSI_V2” is a sign-up / sign-in built-in policy that I had previously created.

This results in:

The template "ASP.NET Core Web App (Model-View-Controller, Microsoft identity platform)" was created successfully.This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/5.0-third-party-notices for details.Processing post-creation actions...
Running 'dotnet restore' on C:\...\Microsoft.Identity.Web\mvc2 - B2C\mvc2 - B2C.csproj...
Determining projects to restore...
Restored C:\Src\...\Microsoft.Identity.Web\mvc2 - B2C\mvc2 - B2C.csproj (in 1.93 sec).
Restore succeeded.

Then:

dotnet buildMicrosoft (R) Build Engine version 16.8.0-preview-20451-02+51a1071f8 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
mvc2 - B2C -> C:\Src\...\Microsoft.Identity.Web\mvc2 - B2C\bin\Debug\netcoreapp3.1\mvc2 - B2C.dll
mvc2 - B2C -> C:\Src\...\Microsoft.Identity.Web\mvc2 - B2C\bin\Debug\net5.0\mvc2 - B2C.dll
mvc2 - B2C -> C:\Src\...\Microsoft.Identity.Web\mvc2 - B2C\bin\Debug\netcoreapp3.1\mvc2 - B2C.Views.dll
mvc2 - B2C -> C:\Src\...\Microsoft.Identity.Web\mvc2 - B2C\bin\Debug\net5.0\mvc2 - B2C.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.06

Then:

dotnet run -f net5.0Building...
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Src\VisualStudio2019\Microsoft.Identity.Web\mvc2 - B2C
info: Microsoft.Hosting.Lifetime[0]

And navigating to http://localhost:5000 as above gives:

Click “Sign in”.

This takes us to the B2C login page as expected:

Sign in with a B2C local account:

All good!

--

--

Rory Braybrook
The new control plane

NZ Microsoft Identity dude and MVP. Azure AD/B2C/ADFS/Auth0/identityserver. StackOverflow: https://bit.ly/2XU4yvJ Presentations: http://bit.ly/334ZPt5