IdentityServer4 with PostgreSQL as a persistence store

Aspram Shadyan
3 min readMay 28, 2019

--

Recently I’ve got addicted to open source technology. The first one I used was IdentityServer4. I have always been using Microsoft products and as a .NET developer, I was sceptical related to using other SQL databases other than SQL Server. After going deeper into Postgres with its programmer friendly documentation, ORDBMS support, rich collection of indexes and extensibility, finally I have decided to give it a try.

Compared to SQL Server we have a big gap on how to use this tools together. So here is my guide. I hope it will be helpful.

Everybody who has ever come across to IdentityServer and Postgres, perhaps knows that they both have a great documentation and give all the interfaces to the programmer to implement his or her own logic.

Let’s come back to our muttons:

  1. Create a new ASP.NET Core Web Application. Currently I am using Visual Studio 2019 Preview version. Everything described here can freely be done using VS 2017.

After specifying the name and the path of the project choose the API type of the project:

2. Add a new .NET Standard class library to your solution as a data access layer:

3. Have installed the latest versions of these Nuget packages in your data access layer:

IdentityServer4

IdentityServer4.EntityFramework.Storage

Npgsql.EntityFrameworkCore.PostgreSQL

And also this package in your service layer:

IdentityServer4.EntityFramework.

4. Create a new Config.cs class in your data access layer and add the following code (note here I have implemented Client Credentials OAuth 2.0 grant):

5. Create your custom Db context:

6․ Create another class which will initialize the database (Code first approach of Entity Framework):

7․ Add the connection string to appsettings.json file (if you have production and development or some other environments, then configure the connection string for them also):

“ConnectionStrings”: { “IdentityServerConnection”:     “host=localhost;database=<YourAuthDatabase>;user id=<username>;Password=<password>;Command Timeout=0”
}

8․ Configure you Startup.cs class by adding the following lines to ConfigureServices method:

9. And the lines below to your Configure method. Note context property is of type AuthDbContext, it is a parameter in Configure method.

10. Finally add the migrations for IdentityServer4 PersistedGrantDbContext and ConfigurationDbContext database contexts from package manager console (do not forget to choose your data access layer as a default project in package manager console):

Add-Migration InitialCreate -Context PersistedGrantDbContext -Output Migrations/PersistedGrantDbAdd-Migration InitialCreate -Context ConfigurationDbContext -Output Migrations/ConfigurationDb

11. Build and run your project. Refresh and check your databases in Postgres, your persistence DB should be created automatically. You can check it with this get call:

http://localhost:5000/.well-known/openid-configuration

Get access tokens with this post call:

http://localhost:5000/connect/tokenBody:
{
"client_id": "YourCustomAPI",
"client_secret": "secret", "grant_type": "client_credentials", "scope": "yourcustomapi"
}

Note that the body needs to be in ‘x-www-form-urlencoded’ format.

You can find an extended working version following the link below (For my application needs I have implemented Resource Owner Password Credentials Grant. You can easily override this):

--

--