-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
106 lines (85 loc) · 3.5 KB
/
Program.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using SelenicSparkApp.CustomClasses;
using SelenicSparkApp.CustomClasses.SelenicSparkApp.CustomClasses;
using SelenicSparkApp.Data;
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.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
options.Lockout.MaxFailedAccessAttempts = 5;
})
.AddRoles<IdentityRole>()
.AddSignInManager<EmailSignInManager>() // Custom sign-in manager
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/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.UseStatusCodePagesWithRedirects("/Home/Error?code={0}");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<UserBanManager>();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "posts",
pattern: "{controller=Posts}/{action=Index}/{page:int}"); // Results in '?page=INT' and '/?page=INT'
app.MapRazorPages();
using (var scope = app.Services.CreateScope())
{
// Generate roles if not present
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roles = new string[] { "Admin", "Moderator", "User" };
for (int i = 0; i < roles.Length; i++)
{
if (!await roleManager.RoleExistsAsync(roles[i]))
{
await roleManager.CreateAsync(new IdentityRole(roles[i]));
}
}
// Assign roles to base users (such as "admin@selenicspark.org")
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
// * Would highly recommend storing logins in separate file and accesing them programmatically
// Remember to follow password guidelines when creating password
// [default]: A-Z + a-z + numbers + special characters
// Tuple<...> here: username, email, password, role
Tuple<string, string, string, string>[] baseUsers = new[]
{
new Tuple<string,string,string,string> ("CaptainSelenicSpark", "admin@selenicspark.org", "123456aA_", roles[0]),
};
for (int j = 0; j < baseUsers.Length; j++)
{
if (await userManager.FindByEmailAsync(baseUsers[j].Item2) == null)
{
var user = new IdentityUser()
{
UserName = baseUsers[j].Item1,
Email = baseUsers[j].Item2,
EmailConfirmed = true // ----------- DANGER ZONE ----------- //
};
await userManager.CreateAsync(user, baseUsers[j].Item3);
await userManager.AddToRoleAsync(user, baseUsers[j].Item4);
}
}
}
app.Run();