Save token in Database

Lavanya Chakkaravel 20 Reputation points
2025-04-29T10:39:14.5766667+00:00

Hi,

I am using Microsoft.EntityFrameworkCore (Version 8.0.6) + Microsoft.AspNetCore.Identity.EntityFrameworkCore(Version 8.0.6) + Npgsql.EntityFrameworkCore.PostgreSQL (Version 8).

By using these packages, Is Identity Frmaework have any configuration/method/way to save token in database automatically without any customSignin or custom method or custom configration?

Is that possible to save token automatically generated by Identity authenticator?

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
420 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-29T19:26:47.4366667+00:00

    Identity uses a token cache. in particular you are interested in a distributed cache.

    https://learn.microsoft.com/en-us/entra/msal/dotnet/how-to/token-cache-serialization?tabs=aspnetcore

    Microsoft supplies an implementation using Resdis, SQL Server and NCache:

    https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-9.0

    the sqlserver cache (nuget package):

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.sqlserver?view=netstandard-2.0-pp

    I could not find an open source sql cache. AWS uses either Redis or its managed sqlserver option.

    here is the sqlserver Cache source. you could port to another database:

    https://github.com/dotnet/aspnetcore/tree/main/src/Caching/SqlServer

    note: another common approach is to just store the token in a cookie.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.