-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
99 lines (80 loc) · 3.18 KB
/
Program.cs
File metadata and controls
99 lines (80 loc) · 3.18 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.EntityFrameworkCore;
using FiwFriends.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using FiwFriends.Models;
using FiwFriends.Services;
using DotNetEnv;
Env.Load();
var builder = WebApplication.CreateBuilder(args);
var apiKey = Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("Where's ur api key?????");
builder.Services.AddSingleton(apiKey);
// Enable console logging for debugging
builder.Logging.AddConsole();
// Database connection
builder.Services.AddDbContext<ApplicationDBContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// Register Identity services with additional password policies
builder.Services.AddIdentity<User, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 8;
options.Lockout.AllowedForNewUsers = true;
options.SignIn.RequireConfirmedEmail = false; // Change to true if email confirmation is needed
})
.AddEntityFrameworkStores<ApplicationDBContext>()
.AddDefaultTokenProviders();
builder.Services.AddScoped<CurrentUserService>();
builder.Services.AddScoped<MapperService>();
builder.Services.AddScoped<UpdateFormStatusService>();
// Add memory cache
builder.Services.AddMemoryCache();
// Add session with proper configuration
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(1440); // Set session timeout to 1440 minutes
options.Cookie.HttpOnly = true; // Prevents JavaScript access for security
options.Cookie.IsEssential = true; // Makes the cookie essential
});
// Configure cookie authentication
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30); // Set timeout to 30 minutes
options.SlidingExpiration = true; // Reset expiration on activity
options.LoginPath = "/Auth/Login"; // Redirect to login if not authenticated
options.LogoutPath = "/Auth/Logout";
options.AccessDeniedPath = "/Auth/Login";
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Auth/Login"; // Redirect to login page
options.AccessDeniedPath = "/Auth/Login"; // Redirect if no permission
});
// Add controllers with views
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
// Add HTTP context accessor for services like CurrentUserService
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Post}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();