In Part 1Part 2Part 3, and Part 4 we touched upon various aspects of configuring IdentityServer, OAuth, and OIDC configuration in ASP.NET Core Web API and MVC applications. Although our sample application is working as expected, it stores all the IdentityServer related configuration in-memory. In a more realistic app you would like to store the configuration in some persistent data store such as SQL Server database. In this part we will do just that.

Recollect from Part 1 that the Server project’s Startup class contains the following code in ConfigureServices() :

services.AddIdentityServer()
        .AddInMemoryApiResources
(ServerConfiguration.ApiResources)
        .AddInMemoryApiScopes
(ServerConfiguration.ApiScopes)
        .AddInMemoryIdentityResources
(ServerConfiguration.IdentityResources)
        .AddTestUsers
(ServerConfiguration.TestUsers)
        .AddInMemoryClients
(ServerConfiguration.Clients)
        .AddDeveloperSigningCredential();

Notice all the “AddInMemory…” methods. They store various pieces of configuration such as API resources, API scopes, identity resources, and clients in-memory. We will now change this and store all these pieces in a SQL Server database. To accomplish this task you need to prepare a SQL Server database with a set of tables required by IdentityServer.

Begin by adding the following NuGet packages to the IdentityServerDemo.Server project:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design
  • IdentityServer4.EntityFramework

The Microsoft.EntityFrameworkCore.SqlServer is the EF Core data provider for SQL Server. The Microsoft.EntityFrameworkCore.Design is required because we want to use EF Core migrations. And IdentityServer4.EntityFramework provides support for EF Core.

Then open appsettings.json and store your database connection string as shown below:

"ConnectionStrings": {
  "AppDb": "data source=.;
            initial catalog=Northwind;
            Integrated Security=true"
}

Here, I am using a local installation of SQL Server and Northwind sample database. Make sure to change the connection string as per your setup of SQL Server.

#asp.net #asp.net core #.net #c# #visual studio #csharp

Integrate IdentityServer with AspNet Core
2 Likes15.05 GEEK