Using Microsoft.Identity.Web templates

Rory Braybrook
The new control plane
4 min readAug 31, 2020

You first need .NET Framework 5.0 (currently in preview).

You need to create an “App Registration” in Azure AD to create an entry that then gives you the client and tenant id’s etc.

Check you have the VS previews options checked.

The instructions to build the templates yourself are here and there is more documentation here.

After doing this, I tried to do:

“MVC Web app AAD — single tenant”

dotnet new mvc2 --auth SingleOrg"

But there was nothing matching “mvc2”.

I then installed the templates using nuget as per:

dotnet new --install Microsoft.Identity.Web.ProjectTemplates::0.4.0-previewDetermining projects to restore...
C:\Program Files\dotnet\sdk\5.0.100-rc.1.20452.10\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.EolTargetFrameworks.targets(28,5): warning NETSDK1138: The target framework 'netcoreapp1.0' is out of support and will not receive security updates in the future. Please refer to https://aka.ms/dotnet-core-support for more information about the support policy. [C:\Users\roryb\.templateengine\dotnetcli\v5.0.100-rc.1.20452.10\scratch\restore.csproj]
Restored C:\Users\roryb\.templateengine\dotnetcli\v5.0.100-rc.1.20452.10\scratch\restore.csproj (in 16.75 sec).
Templates Short Name Language Tags
------------------------------------------------ ------------------- ------------ ----------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
WPF Application wpf [C#], VB Common/WPF
WPF Class library wpflib [C#], VB Common/WPF
WPF Custom Control Library wpfcustomcontrollib [C#], VB Common/WPF
WPF User Control Library wpfusercontrollib [C#], VB Common/WPF
Windows Forms (WinForms) Application winforms [C#], VB Common/WinForms
Windows Forms (WinForms) Control Library winformscontrollib [C#], VB Common/WinForms
Windows Forms (WinForms) Class Library winformslib [C#], VB Common/WinForms
Worker Service worker [C#], F# Common/Worker/Web
Unit Test Project mstest [C#], F#, VB Test/MSTest
NUnit 3 Test Project nunit [C#], F#, VB Test/NUnit
NUnit 3 Test Item nunit-test [C#], F#, VB Test/NUnit
xUnit Test Project xunit [C#], F#, VB Test/xUnit
Razor Component razorcomponent [C#] Web/ASP.NET
Razor Page page [C#] Web/ASP.NET
MVC ViewImports viewimports [C#] Web/ASP.NET
MVC ViewStart viewstart [C#] Web/ASP.NET
Blazor Server App (Microsoft identity platform) blazorserver2 [C#] Web/Blazor
Blazor Server App blazorserver [C#] Web/Blazor
Blazor WebAssembly App blazorwasm2 [C#] Web/Blazor/WebAssembly
Blazor WebAssembly App blazorwasm [C#] Web/Blazor/WebAssembly
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller, ... mvc2 [C#] Web/MVC
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App (Microsoft identity plat... webapp2 [C#] Web/MVC/Razor Pages
ASP.NET Core Web App webapp [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library
ASP.NET Core Web API (Microsoft.Identity.Plat... webapi2 [C#] Web/WebAPI
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
ASP.NET Core gRPC Service grpc [C#] Web/gRPC
dotnet gitignore file gitignore Config
global.json file globaljson Config
NuGet Config nugetconfig Config
Dotnet local tool manifest file tool-manifest Config
Web Config webconfig Config
Solution File sln Solution
Protocol Buffer File proto Web/gRPC
Examples:
dotnet new mvc --auth Individual
dotnet new nunit-test
dotnet new --help

Running:

dotnet -new — help as above shows the following .NET Core projects:

ASP.NET Core Empty                                    web                      [C#], F#          Web/Empty
ASP.NET Core Web App (Model-View-Controller, ... mvc2 [C#] Web/MVC
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App (Microsoft identity plat... webapp2 [C#] Web/MVC/Razor Pages
ASP.NET Core Web App webapp [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library
ASP.NET Core Web API (Microsoft.Identity.Plat... webapi2 [C#] Web/WebAPI
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
ASP.NET Core gRPC Service grpc [C#] Web/gRPC

Let’s use “mvc2”.

The authentication parameters for “mvc2” are:

Options:                                                                                                                
-au|--auth The type of authentication to use
None - No authentication
Individual - Individual authentication
IndividualB2C - Individual authentication with Azure AD B2C
SingleOrg - Organizational authentication for a single tenant
MultiOrg - Organizational authentication for multiple tenants
Windows - Windows authentication
Default: None

There are two ways to go from here.

Running:

dotnet new mvc2 --auth SingleOrg

will create a project in the directory that you run the command from that authenticates against a single tenant and you can then load the project into VS and manually alter the appsettings.json file to match the “App Registration” details

or

You can add all the parameters to the command line command i.e.

dotnet new mvc2 
--framework net5.0
--auth SingleOrg
--domain tenant.onmicrosoft.com
--client-id bb2...07e
--tenant-id 00d5...c79
--callback-path /signin-oidc

and this will create the settings for you.

Then:

dotnet build

And finally:

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: D:\Src\VS2019\Microsoft.ID.Web\mvc-single-org-via-template1

Navigating to:

http://localhost:5000

will bring up the project and you can then authenticate.

Note: If you get the following error:

IOException: IDX20807: Unable to retrieve document from: ‘[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. HttpResponseMessage: ‘[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]', HttpResponseMessage.Content: ‘[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'.

add the following to Startup.cs.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
IdentityModelEventSource.ShowPII = true; }

That will show you the error / exception.

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