I am working on a .NET application using OpenIddict for authentication and MongoDB for token storage. Despite configuring the application to use reference tokens and setting up data protection with both X.509 certificates and development certificates, I am facing an issue where tokens are not persistent across application restarts. The tokens are being removed upon restarting the application. Here is the relevant part of my configuration:
services.AddOpenIddict()
.AddCore(options => { options.UseMongoDb().UseDatabase(database); })
.AddServer(options =>
{
options.UseDataProtection();
options
.AllowClientCredentialsFlow()
.AllowAuthorizationCodeFlow()
.AllowRefreshTokenFlow();
options
.UseReferenceRefreshTokens()
.UseReferenceAccessTokens()
.SetTokenEndpointUris("/connect/token")
.SetAuthorizationEndpointUris("/connect/authorize")
.SetUserinfoEndpointUris("/connect/userinfo")
.SetIntrospectionEndpointUris("/introspection");
options
.AddDevelopmentEncryptionCertificate()
.AddDevelopmentSigningCertificate();
options
.UseAspNetCore()
.EnableTokenEndpointPassthrough()
.EnableAuthorizationEndpointPassthrough()
.EnableUserinfoEndpointPassthrough()
.DisableTransportSecurityRequirement();
})
.AddValidation(options =>
{
options.UseDataProtection();
options.UseLocalServer();
options.UseAspNetCore();
options.SetIssuer("http://localhost:5000");
});