I am working on a project which is based on micro-service architecture and use the identity server 5.
I set up JWT authentication using the Identity Server 5 as follows:
public static IServiceCollection AddDefaultAuthentication(this IHostApplicationBuilder builder)
{
var services = builder.Services;
var configuration = builder.Configuration;
// {
// "Identity": {
// "Url": "http://identity",
// "Audience": "basket"
// }
// }
var identitySection = configuration.GetSection("Identity");
if (!identitySection.Exists())
{
// No identity section, so no authentication
return services;
}
// prevent from mapping "sub" claim to nameidentifier.
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
services.AddAuthentication().AddJwtBearer(options =>
{
var identityUrl = identitySection.GetRequiredValue("Url");
var audience = identitySection.GetRequiredValue("Audience");
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = audience;
options.TokenValidationParameters = new TokenValidationParameters
{
#if DEBUG
//Needed if using Android Emulator Locally. See https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/local-web-services?view=net-maui-8.0#android
ValidIssuers = [identityUrl, "https://10.0.2.2:5243"],
#else
ValidIssuers = [identityUrl],
#endif
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Constants.SecurityKey)),
ValidTypes = new[] { "at+jwt" },
};
});
services.AddAuthorization();
return services;
}
I want to get access token. But my api is not working.
api.MapGet("/token", async (HttpContext context) => await context.GetTokenAsync("access_token"));
How can I get access token?