mirror of
https://github.com/aliasvault/aliasvault.git
synced 2026-05-07 14:56:02 -04:00
102 lines
3.3 KiB
C#
102 lines
3.3 KiB
C#
using System.Data.Common;
|
|
using AliasServerDb;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AliasVault.Admin2.Auth;
|
|
using AliasVault.Admin2.Main;
|
|
using AliasVault.Admin2.Services;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
|
|
builder.Services.AddCascadingAuthenticationState();
|
|
builder.Services.AddScoped<UserService>();
|
|
builder.Services.AddScoped<JsInvokeService>();
|
|
builder.Services.AddScoped<GlobalNotificationService>();
|
|
builder.Services.AddScoped<IdentityRedirectManager>();
|
|
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultScheme = IdentityConstants.ApplicationScheme;
|
|
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
|
|
})
|
|
.AddIdentityCookies();
|
|
|
|
builder.Services.ConfigureApplicationCookie(options =>
|
|
{
|
|
// Change the login path to /user/login
|
|
options.LoginPath = "/user/login";
|
|
});
|
|
|
|
// We use dbContextFactory to create a new instance of the DbContext for every place that needs it
|
|
// as otherwise concurrency issues may occur if we use a single instance of the DbContext across the application.
|
|
builder.Services.AddSingleton<DbConnection>(container =>
|
|
{
|
|
var configFile = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
|
|
var connection = new SqliteConnection(configFile.GetConnectionString("AliasServerDbContext"));
|
|
connection.Open();
|
|
|
|
return connection;
|
|
});
|
|
|
|
builder.Services.AddDbContextFactory<AliasServerDbContext>((container, options) =>
|
|
{
|
|
var connection = container.GetRequiredService<DbConnection>();
|
|
options.UseSqlite(connection).UseLazyLoadingProxies();
|
|
});
|
|
|
|
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
|
builder.Services.AddIdentityCore<AdminUser>(options =>
|
|
{
|
|
options.Password.RequireDigit = false;
|
|
options.Password.RequireLowercase = false;
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
options.Password.RequireUppercase = false;
|
|
options.Password.RequiredLength = 8;
|
|
options.Password.RequiredUniqueChars = 0;
|
|
options.SignIn.RequireConfirmedAccount = false;
|
|
})
|
|
.AddRoles<AdminRole>()
|
|
.AddEntityFrameworkStores<AliasServerDbContext>()
|
|
.AddSignInManager()
|
|
.AddDefaultTokenProviders();
|
|
|
|
builder.Services.AddSingleton<IEmailSender<AdminUser>, IdentityNoOpEmailSender>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// 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.UseStaticFiles();
|
|
app.UseAntiforgery();
|
|
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
// Add additional endpoints required by the Identity /Account Razor components.
|
|
app.MapAdditionalIdentityEndpoints();
|
|
|
|
app.Run();
|