| [877c13c] | 1 | using ChapterX.Application;
|
|---|
| 2 | using ChapterX.Infrastructure;
|
|---|
| [e294f7d] | 3 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|---|
| 4 | using Microsoft.IdentityModel.Tokens;
|
|---|
| [877c13c] | 5 | using System.Reflection;
|
|---|
| [e294f7d] | 6 | using System.Text;
|
|---|
| [877c13c] | 7 |
|
|---|
| 8 | var builder = WebApplication.CreateBuilder(args);
|
|---|
| 9 |
|
|---|
| 10 | builder.Services.AddControllers();
|
|---|
| 11 | builder.Services.AddEndpointsApiExplorer();
|
|---|
| 12 | builder.Services.AddSwaggerGen(options =>
|
|---|
| 13 | {
|
|---|
| 14 | options.CustomSchemaIds(type => type.FullName);
|
|---|
| [e294f7d] | 15 | options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|---|
| 16 | {
|
|---|
| 17 | Name = "Authorization",
|
|---|
| 18 | Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http,
|
|---|
| 19 | Scheme = "Bearer",
|
|---|
| 20 | BearerFormat = "JWT",
|
|---|
| 21 | In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
|---|
| 22 | Description = "Enter your JWT token"
|
|---|
| 23 | });
|
|---|
| 24 | options.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
|
|---|
| 25 | {
|
|---|
| 26 | {
|
|---|
| 27 | new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|---|
| 28 | {
|
|---|
| 29 | Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
|---|
| 30 | {
|
|---|
| 31 | Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
|---|
| 32 | Id = "Bearer"
|
|---|
| 33 | }
|
|---|
| 34 | },
|
|---|
| 35 | Array.Empty<string>()
|
|---|
| 36 | }
|
|---|
| 37 | });
|
|---|
| [877c13c] | 38 | });
|
|---|
| 39 |
|
|---|
| [e294f7d] | 40 | builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|---|
| 41 | .AddJwtBearer(options =>
|
|---|
| 42 | {
|
|---|
| 43 | options.TokenValidationParameters = new TokenValidationParameters
|
|---|
| 44 | {
|
|---|
| 45 | ValidateIssuer = true,
|
|---|
| 46 | ValidateAudience = true,
|
|---|
| 47 | ValidateLifetime = true,
|
|---|
| 48 | ValidateIssuerSigningKey = true,
|
|---|
| 49 | ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|---|
| 50 | ValidAudience = builder.Configuration["Jwt:Audience"],
|
|---|
| 51 | IssuerSigningKey = new SymmetricSecurityKey(
|
|---|
| 52 | Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
|
|---|
| 53 | };
|
|---|
| 54 | });
|
|---|
| [877c13c] | 55 | builder.Services.AddAuthorization();
|
|---|
| 56 |
|
|---|
| 57 | builder.Services.AddApplication();
|
|---|
| 58 | builder.Services.AddInfrastructure(builder.Configuration);
|
|---|
| 59 |
|
|---|
| 60 | var app = builder.Build();
|
|---|
| 61 |
|
|---|
| 62 | if (app.Environment.IsDevelopment())
|
|---|
| 63 | {
|
|---|
| 64 | app.UseSwagger();
|
|---|
| 65 | app.UseSwaggerUI();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | app.UseHttpsRedirection();
|
|---|
| 69 | app.UseAuthentication();
|
|---|
| 70 | app.UseAuthorization();
|
|---|
| 71 | try
|
|---|
| 72 | {
|
|---|
| 73 | app.MapControllers();
|
|---|
| 74 | }
|
|---|
| 75 | catch (ReflectionTypeLoadException ex)
|
|---|
| 76 | {
|
|---|
| 77 | Console.Error.WriteLine("ReflectionTypeLoadException while mapping controllers:");
|
|---|
| 78 | foreach (var loaderEx in ex.LoaderExceptions ?? [])
|
|---|
| 79 | {
|
|---|
| 80 | Console.Error.WriteLine(loaderEx.ToString());
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | throw;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | app.Run();
|
|---|