95 lines
3.0 KiB
C#
95 lines
3.0 KiB
C#
using System.Text;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Nashel.Modules.Catalog.Infrastructure;
|
|
using Nashel.Modules.Catalog.Presentation.Endpoints;
|
|
using Nashel.Modules.Identity.Infrastructure;
|
|
using Nashel.Modules.Identity.Presentation.Endpoints;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Добавление сервисов в контейнер.
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
// Добавляем поддержку JWT Bearer (кнопка Authorize)
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Description = "Введите только токен в поле ниже (без 'Bearer ')"
|
|
});
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[] {}
|
|
}
|
|
});
|
|
|
|
// Подключение XML-документации
|
|
var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml");
|
|
foreach (var xmlFile in xmlFiles)
|
|
{
|
|
options.IncludeXmlComments(xmlFile);
|
|
}
|
|
});
|
|
|
|
// Регистрация модуля Identity
|
|
builder.Services.AddIdentityModule(builder.Configuration);
|
|
|
|
// Регистрация модуля Catalog
|
|
builder.Services.AddCatalogModule(builder.Configuration);
|
|
|
|
// Настройка аутентификации
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
|
|
ValidAudience = builder.Configuration["JwtSettings:Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"] ?? "super_secret_key_change_me_please_this_is_for_development_only_12345"))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Настройка конвейера HTTP-запросов.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Nashel API v1");
|
|
});
|
|
}
|
|
|
|
// Включение аутентификации и авторизации
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
// Маппинг эндпоинтов
|
|
app.MapIdentityEndpoints();
|
|
app.MapCatalogEndpoints();
|
|
|
|
app.Run();
|