Я настроил аутентификацию JWT с использованием сервера идентификации 5 следующим образом: р>
Код: Выделить всё
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;
}
Код: Выделить всё
api.MapGet("/token",
async (HttpContext context) =>
await context.GetTokenAsync("access_token"));
Подробнее здесь: https://stackoverflow.com/questions/788 ... -token-usi