source: iknow-api/Program.cs@ bb656bc

Last change on this file since bb656bc was c2f79d7, checked in by Stefan-Saveski <stefan@…>, 10 months ago

Refactor and enhance application structure

Enhanced error handling in AuthController with detailed exception handling for user registration. Updated UserDTO to improve JSON serialization and renamed enums for clarity. Introduced new entities (Major, PassedSubject, etc.) and seeded initial data in AppDbContext.

Replaced legacy migrations with 20251208203846_InitialCreate, reflecting the updated schema. Fixed typographical errors and improved JSON serialization in Program.cs with case-insensitive property matching and enum handling.

Refactored repository interfaces for better null safety. Improved debugging in AuthService and updated connection strings. Added the PassedSubject entity and cleaned up redundant code and unused imports for better maintainability.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1using System.Text;
2using System.Text.Json.Serialization;
3using iknow_api.Data;
4using iknow_api.Repositories;
5using iknow_api.Services;
6using Microsoft.AspNetCore.Authentication.JwtBearer;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.IdentityModel.Tokens;
9
10var builder = WebApplication.CreateBuilder(args);
11builder.Services.AddAuthentication(options =>
12{
13 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
14 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
15})
16.AddJwtBearer(options =>
17{
18 options.RequireHttpsMetadata = false; // only for development
19 options.SaveToken = true;
20 options.TokenValidationParameters = new TokenValidationParameters
21 {
22 ValidateIssuer = true,
23 ValidIssuer = "iknow-api",
24
25 ValidateAudience = true,
26 ValidAudience = "iknow-api",
27
28 ValidateLifetime = true,
29
30 ValidateIssuerSigningKey = true,
31 IssuerSigningKey = new SymmetricSecurityKey(
32 Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
33 };
34});
35// Add services to the container.
36builder.Services.AddControllers()
37 .AddJsonOptions(options =>
38 {
39 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
40 options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
41 options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
42 });
43builder.Services.AddOpenApi();
44
45// Register DbContext
46builder.Services.AddDbContext<AppDbContext>(options =>
47 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
48
49// Register your services
50builder.Services.AddScoped<IAuthService, AuthService>();
51builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
52builder.Services.AddScoped<IUserService, UserService>();
53
54builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
55builder.Services.AddScoped<IUserRepository, UserRepository>();
56builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
57builder.Services.AddAuthorization();
58var app = builder.Build();
59
60// Configure the HTTP request pipeline.
61if (app.Environment.IsDevelopment())
62{
63 app.MapOpenApi();
64}
65
66app.UseHttpsRedirection();
67app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
68app.UseAuthorization();
69app.MapControllers();
70
71app.Run();
Note: See TracBrowser for help on using the repository browser.