Я увидел, что ASP.NET Core Identity может обеспечить функциональность для авторизация путем сопоставления некоторых конечных точек (
Код: Выделить всё
app.MapIdentityApi();). Однако я хотел бы использовать JWT вместо простых токенов-носителей текстовых строк (которые есть по умолчанию).
Я пытаюсь настроить все это в модульном монолите. На данный момент вот моя установка:
Program.cs
Код: Выделить всё
using ...;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddUserModuleServices(builder.Configuration);
builder.Services.AddAuthorization();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapIdentityApi();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Код: Выделить всё
using ...;
namespace ...;
public static class UserModuleServiceExtensions
{
public static IServiceCollection AddUserModuleServices(
this IServiceCollection services,
ConfigurationManager config)
{
string? connectionString = config.GetConnectionString("UsersConnectionString");
services.AddDbContext(options => options.UseSqlite(connectionString));
// Configure identity
services.AddIdentityCore()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
// Configure JWT Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = config["JwtSettings:Issuer"],
ValidAudience = config["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["JwtSettings:Key"]!))
};
});
return services;
}
}
Необработанное исключение. System.InvalidOperationException: служба для типа «Microsoft.AspNetCore.Identity.IEmailSender`1[namespace...]» не зарегистрирована.
Любые советы ? Что мне не хватает? Как упоминалось ранее, я новичок в .NET Core, поэтому чувствую себя здесь немного потерянным.
Подробнее здесь: https://stackoverflow.com/questions/785 ... -with-jwts