105 lines
3.7 KiB
C#
105 lines
3.7 KiB
C#
|
|
using System.Globalization;
|
||
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Identity;
|
||
|
|
using Microsoft.AspNetCore.Localization;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.Extensions.Options;
|
||
|
|
using MongoDB.Driver;
|
||
|
|
using WorkTracker.Components;
|
||
|
|
using WorkTracker.Components.Account;
|
||
|
|
using WorkTracker.Configuration;
|
||
|
|
using WorkTracker.Data;
|
||
|
|
using WorkTracker.Services.Auth;
|
||
|
|
using WorkTracker.Services.Festivities;
|
||
|
|
using WorkTracker.Services.Settings;
|
||
|
|
|
||
|
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
|
||
|
|
// Add services to the container.
|
||
|
|
builder.Services.AddRazorComponents()
|
||
|
|
.AddInteractiveServerComponents();
|
||
|
|
|
||
|
|
builder.Services.AddCascadingAuthenticationState();
|
||
|
|
builder.Services.AddScoped<IdentityUserAccessor>();
|
||
|
|
builder.Services.AddScoped<IdentityRedirectManager>();
|
||
|
|
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
|
||
|
|
|
||
|
|
builder.Services.AddAuthentication(options =>
|
||
|
|
{
|
||
|
|
options.DefaultScheme = IdentityConstants.ApplicationScheme;
|
||
|
|
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
|
||
|
|
})
|
||
|
|
.AddIdentityCookies();
|
||
|
|
|
||
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
|
||
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
||
|
|
options.UseSqlite(connectionString));
|
||
|
|
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||
|
|
|
||
|
|
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
|
||
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||
|
|
.AddSignInManager()
|
||
|
|
.AddDefaultTokenProviders();
|
||
|
|
|
||
|
|
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
|
||
|
|
builder.Services.AddLocalization();
|
||
|
|
|
||
|
|
builder.Services.Configure<MongoDbOptions>(builder.Configuration.GetSection(MongoDbOptions.SectionName));
|
||
|
|
builder.Services.Configure<SingleUserOptions>(builder.Configuration.GetSection(SingleUserOptions.SectionName));
|
||
|
|
|
||
|
|
builder.Services.AddSingleton<IMongoClient>(sp =>
|
||
|
|
{
|
||
|
|
var options = sp.GetRequiredService<IOptions<MongoDbOptions>>().Value;
|
||
|
|
return new MongoClient(options.ConnectionString);
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.Services.AddSingleton(sp =>
|
||
|
|
{
|
||
|
|
var options = sp.GetRequiredService<IOptions<MongoDbOptions>>().Value;
|
||
|
|
var mongoClient = sp.GetRequiredService<IMongoClient>();
|
||
|
|
return mongoClient.GetDatabase(options.DatabaseName);
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.Services.AddScoped<IAppSettingsService, MongoAppSettingsService>();
|
||
|
|
builder.Services.AddSingleton<IItalianFestivitySource, ItalianFestivitySource>();
|
||
|
|
builder.Services.AddHostedService<SingleUserSeedService>();
|
||
|
|
|
||
|
|
var app = builder.Build();
|
||
|
|
|
||
|
|
var italianCulture = new CultureInfo("it-IT");
|
||
|
|
CultureInfo.DefaultThreadCurrentCulture = italianCulture;
|
||
|
|
CultureInfo.DefaultThreadCurrentUICulture = italianCulture;
|
||
|
|
|
||
|
|
var localizationOptions = new RequestLocalizationOptions
|
||
|
|
{
|
||
|
|
DefaultRequestCulture = new RequestCulture(italianCulture),
|
||
|
|
SupportedCultures = [italianCulture],
|
||
|
|
SupportedUICultures = [italianCulture]
|
||
|
|
};
|
||
|
|
|
||
|
|
// 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.UseHttpsRedirection();
|
||
|
|
|
||
|
|
app.UseRequestLocalization(localizationOptions);
|
||
|
|
|
||
|
|
app.UseAntiforgery();
|
||
|
|
|
||
|
|
app.MapStaticAssets();
|
||
|
|
app.MapRazorComponents<App>()
|
||
|
|
.AddInteractiveServerRenderMode();
|
||
|
|
|
||
|
|
// Add additional endpoints required by the Identity /Account Razor components.
|
||
|
|
app.MapAdditionalIdentityEndpoints();
|
||
|
|
|
||
|
|
app.Run();
|