-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (70 loc) · 3.24 KB
/
Program.cs
File metadata and controls
85 lines (70 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright © 2025 Leek contributors
// SPDX-License-Identifier: GPL-3.0-or-later
using DemoWebApp.Data;
using Leek.AspNet;
using Leek.Core.Providers;
using Leek.Core.Services;
using Leek.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
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.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddPasswordValidator<LeekPasswordValidator<IdentityUser>>();
builder.Services.AddRazorPages();
// bind leek to the existing ef connection, and throw in a wordlist for a bonus example.
ConnectionContext sqliteConnection = new ConnectionBuilder()
.WithSqlite(connectionString)
.Build();
builder.Services.Configure<LeekPasswordValidatorOptions>(opt =>
{
var wordlist = "example-wordlist.txt"; // Replace from config, or use a real wordlist file.
File.WriteAllLines(wordlist, ["password", "123456", "letmein", "qwerty"]);
opt.Connections = [
sqliteConnection,
new ConnectionBuilder().WithWordlistProvider(wordlist).Build(), // nb i wouldnt really recommend this in a production instance, rather you would copy it into a database or use hibp's provider.
new ConnectionBuilder().WithHIBPProvider().Build() // this uses the HIBP k-Anonymity provider, which does not require a connection string.
];
});
// register leek services needed for the connections defined above.
builder.Services.AddLeekServices()
.AddDatabaseProvider()
.AddHIBPProvider()
.AddWordlistReader()
.AddWordlistProvider();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// 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.UseRouting();
app.UseAuthorization();
app.MapStaticAssets();
app.MapRazorPages()
.WithStaticAssets();
// migrate the database if needed
using (var scope = app.Services.CreateScope())
{
// migrate the leek db first - no migrations currently configured and only works with a new db
var databaseProvider = scope.ServiceProvider.GetRequiredService<IEnumerable<IDataProvider>>().First(x => x is DatabaseProvider)
?? throw new InvalidOperationException("No database provider found.");
using var ctx = ((DatabaseProvider)databaseProvider).CreateDbContext(sqliteConnection);
await ctx.Database.EnsureCreatedAsync();
// asp supports migrations, so add over leek
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();