Files
BirthList/src/BirthList.Web/Program.cs
T
Arne Moerman fa704ab996
Build and Push Docker Image / build-and-push (push) Successful in 23s
Add user profile fields and completion prompt
Added `FirstName`, `LastName`, and `Address` fields to `ApplicationUser` for extended user profiles. Updated `Index.razor` to include these fields in the profile form with validation. Introduced `ProfileCompletionService` to check profile completeness and added a prompt in `TopBar.razor` for incomplete profiles.

Created migration `20260518213355_AddUserProfileFields` to update the database schema. Updated `RegistryService` to use new fields for user display names. Registered `ProfileCompletionService` and `DbContextFactory` in `Program.cs`. Styled the profile completion banner in `TopBar.razor.css`.
2026-05-18 23:56:28 +02:00

250 lines
8.0 KiB
C#

using System.Data;
using BirthList.Infrastructure.Persistence;
using BirthList.Web.Authorization;
using BirthList.Web.Components;
using BirthList.Web.Components.Account;
using BirthList.Web.Configuration;
using BirthList.Web.Data;
using BirthList.Web.Features.Registries;
using BirthList.Web.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddHttpClient("RegistryMetadata", client =>
{
client.Timeout = TimeSpan.FromSeconds(10);
});
builder.Services.Configure<SmtpOptions>(builder.Configuration.GetSection("Smtp"));
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddScoped<RegistryAuthorizationService>();
builder.Services.AddScoped<OwnerBootstrapService>();
builder.Services.AddScoped<RegistryService>();
builder.Services.AddScoped<RegistryMetadataService>();
builder.Services.AddScoped<RegistryThemeService>();
builder.Services.AddScoped<RegistryUserContext>();
builder.Services.AddScoped<SmtpConfigurationStatusService>();
var googleClientId = builder.Configuration["Authentication:Google:ClientId"];
var googleClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
var microsoftClientId = builder.Configuration["Authentication:Microsoft:ClientId"];
var microsoftClientSecret = builder.Configuration["Authentication:Microsoft:ClientSecret"];
var authBuilder = builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
});
authBuilder.AddIdentityCookies();
if (!string.IsNullOrWhiteSpace(googleClientId) && !string.IsNullOrWhiteSpace(googleClientSecret))
{
authBuilder.AddGoogle(options =>
{
options.ClientId = googleClientId;
options.ClientSecret = googleClientSecret;
});
}
if (!string.IsNullOrWhiteSpace(microsoftClientId) && !string.IsNullOrWhiteSpace(microsoftClientSecret))
{
authBuilder.AddMicrosoftAccount(options =>
{
options.ClientId = microsoftClientId;
options.ClientSecret = microsoftClientSecret;
});
}
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
var dataProvider = builder.Configuration["Data:Provider"] ?? "SqlServer";
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
if (string.Equals(dataProvider, "SqlServer", StringComparison.OrdinalIgnoreCase))
{
options.UseSqlServer(connectionString);
return;
}
options.UseSqlite(connectionString);
});
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
{
if (string.Equals(dataProvider, "SqlServer", StringComparison.OrdinalIgnoreCase))
{
options.UseSqlServer(connectionString);
return;
}
options.UseSqlite(connectionString);
}, ServiceLifetime.Scoped);
builder.Services.AddDbContext<RegistryDbContext>(options =>
{
if (string.Equals(dataProvider, "SqlServer", StringComparison.OrdinalIgnoreCase))
{
options.UseSqlServer(connectionString);
return;
}
options.UseSqlite(connectionString);
});
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentityCore<ApplicationUser>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();
builder.Services.AddScoped<SmtpEmailSender>();
builder.Services.AddScoped<IEmailSender<ApplicationUser>>(serviceProvider => serviceProvider.GetRequiredService<SmtpEmailSender>());
builder.Services.AddScoped<ProfileCompletionService>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var applicationDbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await applicationDbContext.Database.MigrateAsync();
var registryDbContext = scope.ServiceProvider.GetRequiredService<RegistryDbContext>();
var registryMigrations = registryDbContext.Database.GetMigrations();
if (registryMigrations.Any())
{
await registryDbContext.Database.MigrateAsync();
}
else
{
var databaseCreator = registryDbContext.Database.GetService<IRelationalDatabaseCreator>();
if (!await databaseCreator.ExistsAsync())
{
await registryDbContext.Database.EnsureCreatedAsync();
}
else
{
var platformOwnersTableExists = await TableExistsAsync(registryDbContext, "PlatformOwners");
if (!platformOwnersTableExists)
{
await databaseCreator.CreateTablesAsync();
}
}
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseForwardedHeaders();
var publicUrl = app.Configuration["PublicUrl"];
if (!string.IsNullOrWhiteSpace(publicUrl) && Uri.TryCreate(publicUrl, UriKind.Absolute, out var publicUri))
{
app.Use(async (context, next) =>
{
context.Request.Scheme = publicUri.Scheme;
context.Request.Host = publicUri.IsDefaultPort
? new Microsoft.AspNetCore.Http.HostString(publicUri.Host)
: new Microsoft.AspNetCore.Http.HostString(publicUri.Host, publicUri.Port);
await next();
});
}
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
app.Run();
static async Task<bool> TableExistsAsync(DbContext context, string tableName)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(tableName);
var connection = context.Database.GetDbConnection();
var closeConnection = connection.State != ConnectionState.Open;
if (closeConnection)
{
await connection.OpenAsync();
}
try
{
await using var command = connection.CreateCommand();
if (context.Database.IsSqlServer())
{
command.CommandText = "SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tableName";
}
else if (context.Database.IsSqlite())
{
command.CommandText = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = @tableName";
}
else
{
throw new NotSupportedException("Unsupported database provider for table existence check.");
}
var parameter = command.CreateParameter();
parameter.ParameterName = "@tableName";
parameter.Value = tableName;
command.Parameters.Add(parameter);
var result = await command.ExecuteScalarAsync();
return result is not null && result != DBNull.Value;
}
finally
{
if (closeConnection)
{
await connection.CloseAsync();
}
}
}