-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (126 loc) · 4.42 KB
/
Program.cs
File metadata and controls
158 lines (126 loc) · 4.42 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using AspNetCoreRateLimit;
using Microsoft.AspNetCore.Authentication.Cookies;
using CloudinaryDotNet;
using DotNetEnv;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
var builder = WebApplication.CreateBuilder(args);
// Load environment variables from .env file
DotNetEnv.Env.Load(); // This will load .env file values into Environment
// Read Cloudinary credentials from environment variables
var cloudinaryAccount = new Account(
Environment.GetEnvironmentVariable("CLOUDINARY_CLOUD_NAME"),
Environment.GetEnvironmentVariable("CLOUDINARY_API_KEY"),
Environment.GetEnvironmentVariable("CLOUDINARY_API_SECRET")
);
var cloudinary = new Cloudinary(cloudinaryAccount);
builder.Services.AddSingleton(cloudinary);
// Add services to the container.
builder.Services.AddControllersWithViews();
// Register MongoDB context
builder.Services.AddSingleton<MongoDbContext>();
// Register IMongoDatabase using MongoDbContext
builder.Services.AddSingleton<IMongoDatabase>(sp =>
{
var mongoDbContext = sp.GetRequiredService<MongoDbContext>();
return mongoDbContext.Database;
});
builder.Services.AddSingleton<IMongoCollection<AdminLog>>(sp =>
{
var database = sp.GetRequiredService<IMongoDatabase>();
return database.GetCollection<AdminLog>("AdminLog");
});
// Add HttpClientFactory
builder.Services.AddHttpClient();
// Add MemoryCache
builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// Add authentication using cookies
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
// Optional Access Denied page
options.Events.OnRedirectToLogin = context =>
{
context.Response.Redirect("/Admin/Login");
return Task.CompletedTask;
};
});
// Configuration options for rate limiting
builder.Services.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting"));
// Configure RateLimit stores
builder.Services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();
builder.Services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
builder.Services.AddInMemoryRateLimiting();
// Add framework services.
builder.Services.AddOptions();
builder.Services.AddControllers();
builder.Services.AddLogging();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowVite", policy =>
{
policy.WithOrigins("http://localhost:5173", "https://www.alanwar.studio")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCors("AllowVite");
app.UseRouting();
// Enable session middleware
app.UseSession();
// Add middleware to enable JSON request handling
app.Use(async (context, next) =>
{
if (context.Request.ContentType == "application/json")
{
context.Request.EnableBuffering();
}
await next();
});
// Enable rate limiting
app.UseIpRateLimiting();
// Update the default route for Access Denied
app.MapControllerRoute(
name: "accessDenied",
pattern: "AccessDenied",
defaults: new { controller = "Home", action = "AccessDenied" });
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Default routes for public pages
app.MapControllerRoute(
name: "default",
pattern: "{action=Index}",
defaults: new { controller = "Home" });
// Admin routes
app.MapControllerRoute(
name: "admin",
pattern: "Admin/Login",
defaults: new { controller = "Admin", action = "Login" });
app.MapControllerRoute(
name: "admin",
pattern: "Admin/{controller=Dashboard}/{action=Index}/{id?}");
app.Run();