Search code examples
asp.net-coreoauth-2.0identityserver4

How to persist user consent information in database Identity Server


I'm using IdentityServer4 and whenever the user login for the first time the application shows consent screen for the scopes of an application to the user which is expected. If the user clicks "yes" and click on remember option the application is not showing the consent screen when the user login for second time onwards which is also expected. Here the problem I'm facing is whenever I restart the IdentityServer (or when I do the deployment) the user consent information is not persisting and it is showing the consent screen again for the user. Can anyone help me to know Is there any way to store the user consent information into DataBase or how can we know where the information is being stored when the application is in running. I did some debugging but couldn't find it. Thanks in advance.


Solution

  • Add reference IdentityServer4.EntityFramework.Storage nuget package to identity server 4 project, Then in startup.cs

    service.AddIdentityServer((options) => {})
    .AddPersistedGrantStore<PersistedGrantStore>()
    // Add other services.
    

    The PersistedGrantStore requires PersistedGrantDbContext uses EntityFramework and requires the DbContext to be configured (same way you configure other EntityFramework DbContext). For example to use SQL Server

    services.AddDbContext<PersistedGrantDbContext>(options =>
      options.UseSqlServer(connectionString));
    

    You can use dotnet-ef command tool to create and initialize the tables in the database.

    You can also have your own implementation of IPersistedGrantStore service.