My Project 1.0
Open Source Encrypted Vault Storage
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
4using Microsoft.AspNetCore.Identity;
5
6// ============================================================
7// Application Entry Point
8// Configures and runs the Ignivault Web API.
9// ============================================================
10
11var builder = WebApplication.CreateBuilder(args);
12var configuration = builder.Configuration;
13
14// --- Configure Kestrel Server ---
15builder.WebHost.ConfigureKestrel(options =>
16{
17 options.Limits.MaxRequestBodySize = 104_857_600; // 100 MB
18});
19
20// --- Configure Logging ---
21builder.Logging.ClearProviders();
22builder.Logging.AddConsole();
23builder.Logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
24
25// --- Register Services for Dependency Injection ---
26
27// Registers the custom global exception handler.
28builder.Services.AddExceptionHandler<ExceptionHandler>();
29builder.Services.AddProblemDetails();
30
31// Registers controllers and services for API documentation (Swagger).
32builder.Services.AddControllers();
33builder.Services.AddEndpointsApiExplorer();
34builder.Services.AddSwaggerGen();
35
36// Registers all custom application services using extension methods for cleanliness.
37builder.Services.AddDatabaseServices(configuration);
38builder.Services.AddIdentityAndAuthentication(configuration);
39builder.Services.AddCorsPolicies(configuration);
40builder.Services.AddRepositoryServices();
41builder.Services.AddApplicationServices();
42
43var app = builder.Build();
44
45// --- Initial Database Data ---
46await SeedRolesAsync(app.Services);
48
49
50// ============================================================
51// Configure the HTTP Request Pipeline
52// ============================================================
53
54// Use the registered global exception handler.
55app.UseExceptionHandler();
56
57// Enable Swagger UI only in the development environment.
58if (app.Environment.IsDevelopment())
59{
60 app.UseSwagger();
61 app.UseSwaggerUI();
62}
63
64app.UseRouting();
65
66// Apply the configured CORS policy.
67app.UseCors("AllowClient");
68
69app.UseHttpsRedirection();
70
71app.UseAuthentication();
72
73app.UseAuthorization();
74
75app.MapControllers();
76
77app.Run();
78
79
80//Ensures that the default "Admin" and "User" roles exist in the database.
81static async Task SeedRolesAsync(IServiceProvider services)
82{
83 using var scope = services.CreateScope();
84 var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
85
86 string[] roles = { "Admin", "User" };
87
88 foreach (var role in roles)
89 {
90 if (!await roleManager.RoleExistsAsync(role))
91 {
92 await roleManager.CreateAsync(new IdentityRole(role));
93 }
94 }
95}
96
97//Creates default administrator accounts based on the configuration in app-settings.json.
98static async Task CreateDefaultAdminsAsync(IServiceProvider services)
99{
100 using var scope = services.CreateScope();
101 var userManager = scope.ServiceProvider.GetRequiredService<UserManager<LoginUser>>();
102 var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
103 var adminUsers = configuration.GetSection("AdminUsers").Get<List<AdminUserConfig>>();
104
105 if (adminUsers == null || !adminUsers.Any())
106 {
107 Console.WriteLine("No admin users configured.");
108 return;
109 }
110
111 Console.WriteLine("Creating Default Admin Accounts...");
112
113 foreach (var adminConfig in adminUsers)
114 {
115 if (string.IsNullOrEmpty(adminConfig.Email) || string.IsNullOrEmpty(adminConfig.Password)) continue;
116
117 if (await userManager.FindByEmailAsync(adminConfig.Email) == null)
118 {
119 var salt = System.Security.Cryptography.RandomNumberGenerator.GetBytes(16);
120
121 var adminUser = new LoginUser
122 {
123 UserName = adminConfig.Email,
124 Email = adminConfig.Email,
125 EmailConfirmed = true,
126 KeySalt = salt
127 };
128
129 var result = await userManager.CreateAsync(adminUser, adminConfig.Password);
130
131 if (result.Succeeded)
132 {
133 await userManager.AddToRoleAsync(adminUser, "Admin");
134 await userManager.AddToRoleAsync(adminUser, "User"); // Also add User role
135 Console.WriteLine($"Admin User '{adminConfig.Email}' created successfully.");
136 }
137 }
138 }
139}
140
144internal class AdminUserConfig
145{
146 public string? Email { get; set; }
147 public string? Password { get; set; }
148}
LoginUser extends the IdentityUser class to include additional properties specific to the application...
Definition LoginUser.cs:7
var builder
Definition Program.cs:18
var configuration
Definition Program.cs:12
await CreateDefaultAdminsAsync(app.Services)
var app
Definition Program.cs:43
await SeedRolesAsync(app.Services)