'Tokens does not store in Database by use of Ef core 6 and microsoft identity
I followed the document to set up Openiddict and connect it to Aspnet identity, but it seems to me Openiddict cant store tokens in the database( AspNetUserTokens is empty while access and refresh tokens are generated correctly), and that cause user
var info = await HttpContext.AuthenticateAsync(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
var user =await _signInManager.ValidateSecurityStampAsync(info.Principal);
be an empty object while trying to use refresh token to generate another access token.
I looked out everywhere to find a response to this issue and also check out Openiddict source code but unfortunately couldn't sort it out
public class OpenIddictBootstrapper : IBootstarpper { public void RegisterComponents(IServiceCollection services, IConfiguration config, IWebHostEnvironment env) { services.AddOpenIddict()
// Register the OpenIddict core components.
.AddCore(options =>
{
// Configure OpenIddict to use the Entity Framework Core stores and models.
// Note: call ReplaceDefaultEntities() to replace the default entities.
options.UseEntityFrameworkCore()
.UseDbContext<ApplicationDbContext>();
})
// Register the OpenIddict server components.
.AddServer(options =>
{
// Enable the token endpoint.
options.SetTokenEndpointUris("/Token");
// Enable the password flow.
options.AllowPasswordFlow().AllowRefreshTokenFlow();
// Accept anonymous clients (i.e clients that don't send a client_id).
options.AcceptAnonymousClients();
// Encryption and signing of tokens
options
.AddEphemeralEncryptionKey()
.AddEphemeralSigningKey();
// Register the ASP.NET Core host and configure the ASP.NET Core options.
options.UseAspNetCore()
.EnableTokenEndpointPassthrough()
.DisableTransportSecurityRequirement();
//Disable encryption to Debug purposes
if (env.IsDevelopment())
options.DisableAccessTokenEncryption();
//options.AddEventHandler<OpenIddictServerEvents.ExtractTokenRequestContext>(options =>
// options.UseSingletonHandler<ExtractTokenRequestHandler>());
})
// Register the OpenIddict validation components.
.AddValidation(options =>
{
// Import the configuration from the local OpenIddict server instance.
options.UseLocalServer();
// Register the ASP.NET Core host.
options.UseAspNetCore();
});
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.UserNameClaimType = OpenIddictConstants.Claims.Username;
options.ClaimsIdentity.UserIdClaimType = OpenIddictConstants.Claims.Subject;
options.ClaimsIdentity.RoleClaimType = OpenIddictConstants.Claims.Role;
options.ClaimsIdentity.EmailClaimType = OpenIddictConstants.Claims.Email;
options.ClaimsIdentity.SecurityStampClaimType = "secret_value";
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
