-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (110 loc) · 3.68 KB
/
Program.cs
File metadata and controls
145 lines (110 loc) · 3.68 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
using CodePulse.API.Data;
using CodePulse.API.Repositories.Implementation;
using CodePulse.API.Repositories.Interace;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.Security.Claims;
using CodePulse.API.Repositories.Implementation;
using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//builder.Services.addsin
//builder.Services.AddAuthentication("").AddJwtBearer("", options => {
// options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters {
// ValidateIssuer = true,
// ValidateAudience = true,
// };
//});
//builder.Services.AddAuthentication("").AddOAuth("", options =>
//{
// //options.ClaimsIssuer = new[] { };
//});
// configure cookies as below
//builder.Services.Configure<CookiePolicyOptions>(options => {
// options.MinimumSameSitePolicy = SameSiteMode.Strict;
// options.Secure = CookieSecurePolicy.Always;
//});
//builder.Services.AddCookiePolicy(options => {
// options.HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always;
// options.MinimumSameSitePolicy = SameSiteMode.Strict;
// options.Secure = CookieSecurePolicy.Always;
// options.CheckConsentNeeded = context => true;
//});
//builder.Services.AddSession(options =>
//{
// options.IOTimeout = new TimeSpan(1000);
// options.Cookie.HttpOnly = true;
// options.Cookie.IsEssential = true;
//});
//var claim = new List<Claim>
//{
// new Claim(ClaimTypes.Role, ""),
//};
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("CodePulseConnectionString"));
});
//builder.Configuration.
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddRateLimiter(_ => _
.AddFixedWindowLimiter(policyName: "fixed", options =>
{
options.PermitLimit = 4;
options.Window = TimeSpan.FromSeconds(12);
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 2;
}));
//builder.WebHost.ConfigureKestrel(options => { options. })
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
//app.UseExceptionHandler("/Error");
//app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseMiddleware<CustomExceptionHandler>();
app.UseMiddleware<CustomHeaderMiddleware>();
var blockedIPs = new[] { "127.0.0.1" }; // Add IPs you want to block
app.UseMiddleware<IPBlockingMiddleware>(blockedIPs);
// enforece cookie policy
//app.UseCookiePolicy();
//app.UseSession();
//app.Map()
app.Use(async (context, next) =>
{
Console.WriteLine($"Request: {context.Request.Method} {context.Request.Path}");
await next(); // Call the next middleware
Console.WriteLine($"Response: {context.Response.StatusCode}");
});
//app.UseCookiePolicy();
//app.UseRouting();
//app.UseRateLimiter();
//app.UseRequestLocalization();
app.UseCors(options =>
{
options.AllowAnyHeader();
options.AllowAnyOrigin();
options.AllowAnyMethod();
});
app.UseRateLimiter();
//app.UseAuthentication();
//app.UseAuthorization();
//app.UseSession();
//app.UseResponseCompression();
//app.UseResponseCaching();
app.MapControllers();
//app.MapFallbackToFile("/app/index.html");
app.Run();