Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityServer4 PersistedGrantDbContext & ConfigurationDbContext

New to IdentityServer 4. I followed the IdentityServer4 EntityFramework sample here on the documentation.

After the migration script is run

dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb

It works and now my application has 3 DB Contexts.

  • ApplicationDbContext
  • PersistedGrantDbContext
  • ConfigurationDbContext

My question is what are the two DB contexts for? What is the difference between the application db context and the other two?

If I update or add any models, do I need to update all three? Or when should I run a migration on the ApplicationDbContext and when to run on the other two.

Any insight or literature on these is appreciated. Thanks.

like image 410
Nicholas Ibarra Avatar asked Aug 08 '17 17:08

Nicholas Ibarra


People also ask

What is PersistedGrantDbContext?

PersistedGrantDbContext - used for temporary operational data such as authorization codes, and refresh tokens.

How do I change migration Assembly?

By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."


1 Answers

Figured it out. Leaving this for anyone confused about this as I was.

There are 3 DB contexts and, as @Jasen mentioned, it is to split up access to the entities, or tables.

IdeneityServer4 + EntityFramework + ASP.NET Identity creates the following tables in the database:

SQL Server Tables

The contexts are used to reference the following:

ApplicationDbContext - responsible for users involved with ASP.NET Identity so tables

  • dbo.AspNetRoleClaims
  • dbo.AspNetRoles
  • dbo.AspNetUserClaims
  • dbo.AspNetUserLogins
  • dbo.AspNetUserRoles
  • dbo.AspNetUsers
  • dbo.AspNetUserTokens

PersistedGrantDbContext - responsible for storing consent, authorization codes, refresh tokens, and reference tokens

  • dbo.PersistedGrants

ConfigurationDbContext - responsible for everything else remaining in the database

So in regards to migrations, if I update any of the AspNet Identity models (i.e. ApplicationUser) then I would run the migration on the ApplicationDbContext. Any client tables or other scopes would be run on the ConfigurationDbContext. And to access the entites (or tables) would be the corresponding context.

like image 199
Nicholas Ibarra Avatar answered Sep 28 '22 00:09

Nicholas Ibarra