Skoruba Identityserver4 Admin
Skoruba Identityserver4 Admin
Documentation
Release dev-doc
1 Overview 3
1.1 Solution structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 IdentityServer4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Asp.Net Core Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Application Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Template uses following list of nuget packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Localizations - labels, messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Administration UI preview 7
3 Installation 9
3.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Installation methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Installation of the Client Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Running in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
i
ii
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
Introduction 1
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
2 Introduction
CHAPTER 1
Overview
STS
Skoruba.IdentityServer4.STS.Identity Quickstart UI for the IdentityServer4 with Asp.Net Core Iden-
tity and EF Core storage
Admin UI
Skoruba.IdentityServer4.Admin ASP.NET Core MVC application that contains Admin UI
Skoruba.IdentityServer4.Admin.BusinessLogic project that contains Dtos, Repositories, Services
and Mappers for the IdentityServer4
Skoruba.IdentityServer4.Admin.BusinessLogic.Identity project that contains Dtos, Reposito-
ries, Services and Mappers for the Asp.Net Core Identity
Skoruba.IdentityServer4.Admin.BusinessLogic.Shared project that contains shared Dtos and Ex-
ceptionHandling for the Business Logic layer of the IdentityServer4 and Asp.Net Core Identity
Skoruba.IdentityServer4.Admin.EntityFramework EF Core data layer that contains Entities for the
IdentityServer4
Skoruba.IdentityServer4.Admin.EntityFramework.Identity EF Core data layer that contains
Entities for the Asp.Net Core Identity
Skoruba.IdentityServer4.Admin.EntityFramework.DbContexts project that contains AdminDb-
Context for the administration
Tests
Skoruba.IdentityServer4.Admin.IntegrationTests xUnit project that contains the integration tests
Skoruba.IdentityServer4.Admin.UnitTests xUnit project that contains the unit tests
The admininistration contains the following sections
3
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
1.2 IdentityServer4
Clients
It is possible to define the configuration according the client type - by default the client types are used:
• Empty
• Web Application - Server side - Implicit flow
• Web Application - Server side - Hybrid flow
• Single Page Application - Javascript - Implicit flow
• Native Application - Mobile/Desktop - Hybrid flow
• Machine/Robot - Resource Owner Password and Client Credentials flow
• TV and Limited-Input Device Application - Device flow
• Actions: Add, Update, Clone, Remove
• Entities: - Client Cors Origins - Client Grant Types - Client IdP Restrictions - Client Post Logout Redirect Uris
- Client Properties - Client Redirect Uris - Client Scopes - Client Secrets
API Resources
• Actions: Add, Update, Remove
• Entities: - Api Claims - Api Scopes - Api Scope Claims - Api Secrets
Identity Resources
• Actions: Add, Update, Remove
• Entities: - Identity Claims
Users
• Actions: Add, Update, Delete
4 Chapter 1. Overview
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
• Change the specific URLs and names for the IdentityServer and Authentication settings in Constants/
AuthenticationConsts or appsettings.json
• Constants/AuthorizationConsts.cs contains configuration of constants connected with authoriza-
tion - definition of the default name of admin policy
• In the controllers is used the policy which name is stored in - AuthorizationConsts.
AdministrationPolicy. In the policy - AuthorizationConsts.AdministrationPolicy is defined required
role stored in - AuthorizationConsts.AdministrationRole.
• With the default configuration, it is necessary to configure and run instance of IdentityServer4. It is possible to
use initial migration for creating the client as it mentioned above
• All labels and messages are stored in the resources .resx - locatated in /Resources
– Client label descriptions from - http://docs.identityserver.io/en/release/reference/client.html
– Api Resource label descriptions from - http://docs.identityserver.io/en/release/reference/api_resource.html
– Identity Resource label descriptions from - http://docs.identityserver.io/en/release/reference/identity_
resource.html
1.8 Tests
6 Chapter 1. Overview
CHAPTER 2
Administration UI preview
• Forms
7
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
Installation
3.1 Requirements
• Install the latest .NET Core 2.x SDK (using older versions may lead to 502.5 errors when hosted on IIS or
application exiting immediately after starting when self-hosted)
Cloning
--adminclientid: [string value] for client name, that is used in the IdentityServer4
˓→configuration
9
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
10 Chapter 3. Installation
CHAPTER 4
• Run entity framework migrations - for instance from Visual Studio command line (Nuget package manager):
:: Add-Migration DbInit -context AdminDbContext -output Data/Migrations Update-Database -context AdminDb-
Context
• Or via dotnet CLI:
:: dotnet ef migrations add DbInit -c AdminDbContext -o Data/Migrations dotnet ef database update -c AdminDb-
Context
Migrations are not a part of the repository - they are ignored in .gitignore.
We suggest to use seed data:
• In Program.cs -> Main, uncomment DbMigrationHelpers.EnsureSeedData(host) or use dot-
net CLI dotnet run /seed
• The Clients and Resources files in Configuration/IdentityServer are the initial data, based on
a sample from IdentityServer4
• The Users file in Configuration/Identity contains the default admin username and password for the
first login
11
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
PostgreSQL
Install following NuGet package:
:: Npgsql.EntityFrameworkCore.PostgreSQL.Design
Find RegisterDbContexts function in Helpers\StartupHelpers.cs
:: services.AddDbContext<AdminDbContext>(options => options.UseSqlServer(configuration.GetConnectionString(ConfigurationCon
optionsSql => optionsSql.MigrationsAssembly(migrationsAssembly)));
and change UseSqlServer to UseNpgsql.
Don’t forget to update your connection string in appsettings.json and (re)generate migrations for new database
SQLite
Install following NuGet package:
:: Microsoft.EntityFrameworkCore.Sqlite.Design
Find RegisterDbContexts function in Helpers\StartupHelpers.cs
:: services.AddDbContext<AdminDbContext>(options => options.UseSqlServer(configuration.GetConnectionString(ConfigurationCon
optionsSql => optionsSql.MigrationsAssembly(migrationsAssembly)));
and change UseSqlServer to UseSqlite.
13
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
Note: Don’t forget to update your connection string in appsettings.json and (re)generate migrations for new database
Note: Don’t forget to update your connection string in appsettings.json and (re)generate migrations for new database
15
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
By default, it’s used as the primary key int, but it’s possible to change it:
How to configure DbContext for ASP.NET Core Identity
• You can setup the DbContext - in Startup.cs:
::
services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int,
UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUser-
Login, UserIdentityRoleClaim, UserIdentityUserToken>();
• Method AddAdminAspNetIdentityServices expects the generic param
TAdminDbContext that inherits from IdentityDbContext and implements interface
IAdminPersistedGrantIdentityDbContext (for operation data connected with Asp.Net Core
Identity)
How to configure Identity primary key data type in ASP.NET Core Identity
• By default, it’s used int as the primary key, but you can change to Guid or string.
How to use for example ‘‘Guid‘‘
1. Change int to Guid in Startup.cs:
Original:
::
services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int,
UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUser-
Login, UserIdentityRoleClaim, UserIdentityUserToken>();
New:
17
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
::
services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<Guid>, Guid, RoleDto<Guid>, Guid, Guid, Guid
UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUser-
Login, UserIdentityRoleClaim, UserIdentityUserToken>();
2. Change int to Guid in all files in folder - Skoruba.IdentityServer4.Admin.
EntityFramework/Entities/Identity:
For example - UserIdentity.cs:
Original:
:: public class UserIdentity : IdentityUser<int> {
}
New:
:: public class UserIdentity : IdentityUser<Guid> {
}
• Change int to Guid in other files in this folder - Skoruba.IdentityServer4.Admin.
EntityFramework/Entities/Identity
### 3. Change int to Guid in all files in folder - Skoruba.IdentityServer4.Admin/Views/Identity:
For example - Role.cshtml:
Original:
:: @model Skoruba.IdentityServer4.Admin.BusinessLogic.Dtos.Identity.RoleDto<int> // . . . @if (!Equality-
Comparer<int>.Default.Equals(Model.Id, default(int)))
New:
:: @model Skoruba.IdentityServer4.Admin.BusinessLogic.Dtos.Identity.RoleDto<Guid> // . . . @if (!Equality-
Comparer<Guid>.Default.Equals(Model.Id, default(Guid)))
• Change int to Guid in other files in this folder - Skoruba.IdentityServer4.Admin/Views/
Identity
4. Change int to Guid in AdminDbContext - Skoruba.IdentityServer4.Admin.
EntityFramework/DbContexts:
Original:
::
public class AdminDbContext [IdentityDbContext<UserIdentity, UserIdentityRole, int, UserIdentityUser-
Claim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>,]
IAdminConfigurationDbContext, IAdminLogDbContext, IAdminPersistedGrantIdentityDbContext
New:
::
public class AdminDbContext [IdentityDbContext<UserIdentity, UserIdentityRole, Guid, UserIdentityUser-
Claim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>,]
IAdminConfigurationDbContext, IAdminLogDbContext, IAdminPersistedGrantIdentityDbContext
5. Change int to Guid in GrantController - Skoruba.IdentityServer4.Admin/Controllers:
Original:
:: public class GrantController : BaseController {
7.1. How to configure Asp.Net Core Identity - database, primary key data type 19
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
8.1 Introduction
Tutorial covers configuration of Admin on Ubuntu 18.04 with fresh instance of PostgreSQL database.
8.2 Prerequisites
21
Skoruba IdentityServer4 Admin Documentation, Release dev-doc
By default everything is configured for Microsoft SQL Server, but fortunately it’s pretty easy to change.
Replace connection strings First change connection strings in src/Skoruba.IdentityServer4.
Admin/appsettings.json and src/Skoruba.IdentityServer4.STS.Identity/appsettings.
json and replace them with following connection string:
:: Server=localhost; User Id=postgres; Database=is4admin; Port=5432; Password=postgres; SSL Mode=Prefer; Trust
Server Certificate=true
Install required packages
Next we need to install PostgreSQL support for EntityFramework Core in Skoruba.IdentityServer4.Admin and Sko-
ruba.IdentityServer4.STS.Identity in order to do that run in each project’s directory:
:: dotnet add src/Skoruba.IdentityServer4.Admin package Npgsql.EntityFrameworkCore.PostgreSQL dotnet add
src/Skoruba.IdentityServer4.Admin package Npgsql.EntityFrameworkCore.PostgreSQL.Design dotnet add
src/Skoruba.IdentityServer4.STS.Identity package Npgsql.EntityFrameworkCore.PostgreSQL dotnet add
src/Skoruba.IdentityServer4.STS.Identity package Npgsql.EntityFrameworkCore.PostgreSQL.Design
Replace UseSqlServer with UseNpgsql
In src/Skoruba.IdentityServer4.Admin and src/Skoruba.IdentityServer4.STS.Identity
in Helpers/StartupHelpers.cs replace all occurences of UseSqlServer with UseNpgsql. This will
inform EntityFramework that PostgreSQL will be used instead of SQL Server.
There are many more steps required before IS4 and Admin panel are sufficiently hardened to be used in production
scenario. Please bear in mind that this tutorial serves only as a quickstart.