-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (61 loc) · 2.43 KB
/
Copy pathProgram.cs
File metadata and controls
83 lines (61 loc) · 2.43 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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Assignment1.Model;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Assignment1.Pages;
using Assignment1.Services;
using System.Security.Claims;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddScoped<AuditLogger>();
builder.Services.AddSingleton<SessionTracker>();
builder.Services.AddSingleton<EmailService>();
// Register the Encryption service
builder.Services.AddScoped<Encryption>();
// ? Register AuthDbContext with connection string
builder.Services.AddDbContext<AuthDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("AuthConnectionString")));
// ? Register Identity with AuthDbContext
builder.Services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Lockout.AllowedForNewUsers = true;
options.Lockout.MaxFailedAccessAttempts = 3; // Lockout after 3 failed attempts
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1); // Lockout duration
})
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddSession(options =>
{
options.Cookie.HttpOnly = true; // Secure the session cookie from JavaScript
options.Cookie.IsEssential = true; // Mark the cookie as essential
options.IdleTimeout = TimeSpan.FromSeconds(10); // Set session expiration
});
builder.Services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = true; // At least one number
options.Password.RequireLowercase = true; // At least one lowercase letter
options.Password.RequireUppercase = true; // At least one uppercase letter
options.Password.RequireNonAlphanumeric = true; // At least one special character
options.Password.RequiredLength = 12; // Minimum length of 12 characters
});
var app = builder.Build();
// Configure middleware
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStatusCodePagesWithRedirects("/errors/{0}");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.Run();