.Net Core 5, Identity Server 4 and Project Templates

.Net Core 5, Identity Server 4 and Project Templates

I have been working with IdentityServer4 for a few years now and I a couple of ideas that I would like to post on my blog. So to get started I have been using the IdentityServer4 templates to create some sample projects to demonstrate how the server, clients and api's work together.

Getting Started

To start I needed an Identity Server and wanted to also use ASP .NET Identity. There is a template "is4aspid" that can be used to create a project with a minimal UI, Identity and Identity Server the following command can be used. In my case as I had not installed the template before hand when running the command I received the following and had to install the templates:

PS C:\github\IdentityServerAspNetIdentity> dotnet new is4aspid -n IdentityServerAspNetIdentity

Welcome to .NET 5.0!
---------------------
SDK Version: 5.0.201

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Getting ready...
Couldn't find an installed template that matches the input, searching online for one that does...
Matches from template source: NuGet
-----------------------------------
Template name "IdentityServer4 with ASP.NET Core Identity" (is4aspid) from author "Brock Allen, Dominick Baier" in pack IdentityServer4.Templates
    To use this template, run the following command and try again:
        dotnet new -i IdentityServer4.Templates::4.0.1

Installing the templates

The previous message points out the templates are not installed. So to install the Identity Server template pack the dotnet new -i command needs to be run as indicated in the output above:

PS C:\GitHub\IdentityServerAspNetIdentity> dotnet new -i IdentityServer4.Templates::4.0.1

  Determining projects to restore...
  Restored C:\Users\peteb\.templateengine\dotnetcli\v5.0.201\scratch\restore.csproj (in 2.72 sec).

Templates                                             Short Name           Language    Tags
----------------------------------------------------  -------------------  ----------  ----------------------
Console Application                                   console              [C#],F#,VB  Common/Console
...
IdentityServer4 with AdminUI                          is4admin             [C#]        Web/IdentityServer4
IdentityServer4 with ASP.NET Core Identity            is4aspid             [C#]        Web/IdentityServer4
IdentityServer4 Empty                                 is4empty             [C#]        Web/IdentityServer4
IdentityServer4 with Entity Framework Stores          is4ef                [C#]        Web/IdentityServer4
IdentityServer4 with In-Memory Stores and Test Users  is4inmem             [C#]        Web/IdentityServer4
IdentityServer4 Quickstart UI (UI assets only)        is4ui                [C#]        Web/IdentityServer4
...

Examples:
    dotnet new mvc --auth Individual
    dotnet new wpflib
    dotnet new --help
    dotnet new is4admin --help

The initial project

To create the server using the template, rerun the dotnet new command:

PS C:\github\IdentityServerAspNetIdentity> dotnet new is4aspid -n IdentityServerAspNetIdentity
The template "IdentityServer4 with ASP.NET Core Identity" was created successfully.

Processing post-creation actions...
Template is configured to run the following action:
Description:
Manual instructions: Seeds the initial user database
Actual command: dotnet run /seed
Do you want to run this action [Y(yes)|N(no)]?
y
Running command 'dotnet run /seed'...
Command succeeded.

Adding the WebApi and ASP .NET MVC projects

To demonstrate how the server interacts with the Api and Clietn applications and how authentication and authorisation is implmented in these projects, WebApi and MVC projects can be created using the templates using the dotnet new command.

First the WebApi:

PS C:\GitHub\IdentityServerAspNetIdentity> dotnet new webapi -n WebApi
The template "ASP.NET Core Web API" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on WebApi\WebApi.csproj...
  Determining projects to restore...
  Restored C:\GitHub\IdentityServerAspNetIdentity\WebApi\WebApi.csproj (in 243 ms).
Restore succeeded.

Now the MVC project:

PS C:\GitHub\IdentityServerAspNetIdentity> dotnet new mvc -n ClientMVC
The template "ASP.NET Core Web App (Model-View-Controller)" 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 ClientMVC\ClientMVC.csproj...
  Determining projects to restore...
  Restored C:\GitHub\IdentityServerAspNetIdentity\ClientMVC\ClientMVC.csproj (in 86 ms).
Restore succeeded.

These projects can now be organised and added to a solution in Visual Studio:

IdSvrExplorer.PNG This solution will be used to build examples for future blog posts, the first being how to hook these 3 projects together to provide authentication and authorisation for the client and api projects.

I hope you find this post useful, the source code for this post can be found here in github