IdentityServer is designed for extensibility, and one of the extensibility points is the storage mechanism used for data that IdentityServer needs.
This quickstart shows how to configure IdentityServer to use EntityFramework (EF) as the storage mechanism for this data (rather than using the in-memory implementations we had been using up until now).

Note

In addition to manually configuring EF support, there is also an IdentityServer template to create a new project with EF support. Use dotnetnewis4ef to create it. See here for more information.

There are two types of data that we are moving to the database.
The first is the configuration data (resources and clients).
The second is operational data that IdentityServer produces as it’s being used (tokens, codes, and consents).
These stores are modeled with interfaces, and we provide an EF implementation of these interfaces in the IdentityServer4.EntityFramework Nuget package.

Get started by adding a reference to the IdentityServer4.EntityFramework Nuget package the IdentityServer project.

The IdentityServer4.EntityFramework package contains entity classes that map from IdentityServer’s models.
As IdentityServer’s models change, so will the entity classes in IdentityServer4.EntityFramework.
As you use IdentityServer4.EntityFramework and upgrade over time, you are responsible for your own database schema and changes necessary to that schema as the entity classes change.
One approach for managing those changes is to use EF migrations, and this quickstart will show how that can be done.
If migrations are not your preference, then you can manage the schema changes in any way you see fit.

Note

SQL scripts for SqlServer are maintained for the entities in IdentityServer4.EntityFramework. They are located here.

In addition to tracking schema changes with EF migrations, we will also use it to create the initial schema in the database.
This requires the use of the EF Core tooling (more details here).
We will add those now, and unfortunately this must be done by hand-editing your .csproj file.
To edit the .csproj by right-click the project and select “Edit projectname.csproj”:

Note

Depending on how you created your initial project for the IdentityServer host, you might already have these tools configured in your csproj file. If they are, you can skip to the next section.

The next step is to replace the current calls to AddInMemoryClients, AddInMemoryIdentityResources, and AddInMemoryApiResources in the ConfigureServices method in Startup.cs.
We will replace them with this code:

The above code is hard-coding a connection string, which you should feel free to change if you wish.
Also, the calls to AddConfigurationStore and AddOperationalStore are registering the EF-backed store implementations.

The “builder” callback function passed to these APIs is the EF mechanism to allow you to configure the DbContextOptionsBuilder for the DbContext for each of these two stores.
This is how our DbContext classes can be configured with the database provider you want to use.
In this case by calling UseSqlServer we are using SqlServer.
As you can also tell, this is where the connection string is provided.

The “options” callback function in UseSqlServer is what configures the assembly where the EF migrations are defined.
EF requires the use of migrations to define the schema for the database.

Note

It is the responsibility of your hosting application to define these migrations, as they are specific to your database and provider.

You should now see a ~/Data/Migrations/IdentityServer folder in the project.
This contains the code for the newly created migrations.

Note

If your database project is a separate class library and you fixed the error ‘Unable to create an object of type ‘<your-name>DbContext’. Add an implementation of ‘IDesignTimeDbContextFactory’ to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.’ by adding implementations of the IDesignTimeDbContextFactory, you will also need implementations of the factory for both the PersistedGrantDbContext as well as the ConfigurationDbContext.

Now that we have the migrations, we can write code to create the database from the migrations.
We will also seed the database with the in-memory configuration data that we defined in the previous quickstarts.

publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv,ILoggerFactoryloggerFactory){// this will do the initial DB populationInitializeDatabase(app);// the rest of the code that was already here// ...}

Now if you run the IdentityServer project, the database should be created and seeded with the quickstart configuration data.
You should be able to use SQL Server Management Studio or Visual Studio to connect and inspect the data.

Note

The above InitializeDatabase helper API is convenient to seed the database, but this approach is not ideal to leave in to execute each time the applicaion runs. Once your database is populated, consider removing the call to the API.

You should now be able to run any of the existing client applications and sign-in, get tokens, and call the API – all based upon the database configuration.

Note

The code as it stands in this section still relies upon Config.cs and its fictitious users Alice and Bob. If your user list is short and static, an adjusted version of Config.cs may suffice, however you may wish to manage a larger and more fluid user list dynamically within a database. ASP.NET Identity is one option to consider, and a sample implementation of this solution is listed among the quickstarts in the next section.