-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathProgram.cs
91 lines (77 loc) · 3.57 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
using System.IO;
using System.Text.Json.Serialization;
using DNTCaptcha.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
ConfigureLogging(builder.Logging, builder.Environment, builder.Configuration);
ConfigureServices(builder.Services, builder.Environment);
var webApp = builder.Build();
ConfigureMiddlewares(webApp, webApp.Environment);
ConfigureEndpoints(webApp);
webApp.Run();
void ConfigureServices(IServiceCollection services, IWebHostEnvironment env)
{
services.AddSwaggerGen();
services.AddDNTCaptcha(options =>
{
// options.UseSessionStorageProvider(); // -> It doesn't rely on the server or client's times. Also it's the safest one.
// options.UseMemoryCacheStorageProvider(); // -> It relies on the server's times. It's safer than the CookieStorageProvider.
options
.UseCookieStorageProvider(
/* If you are using CORS, set it to `None` */) // -> It relies on the server and client's times. It's ideal for scalability, because it doesn't save anything in the server's memory.
// .UseDistributedCacheStorageProvider(); // --> It's ideal for scalability using `services.AddStackExchangeRedisCache()` for instance.
// .UseDistributedSerializationProvider();
// Don't set this line (remove it) to use the installed system's fonts (FontName = "Tahoma").
// Or if you want to use a custom font, make sure that font is present in the wwwroot/fonts folder and also use a good and complete font!
.UseCustomFont(Path.Combine(env.WebRootPath, "fonts", "tahoma.ttf")).AbsoluteExpiration(7)
.RateLimiterPermitLimit(
10) // for .NET 7x, Also you need to call app.UseRateLimiter() after calling app.UseRouting().
.ShowExceptionsInResponse(env.IsDevelopment()).ShowThousandsSeparators(false)
.WithNoise(0.015f, 0.015f, 1, 0.0f).WithEncryptionKey("This is my secure key!")
.WithNonceKey("NETESCAPADES_NONCE")
//.WithCaptchaImageControllerRouteTemplate("my-custom-captcha/[action]")
.InputNames(new DNTCaptchaComponent
{
CaptchaHiddenInputName = "DNTCaptchaText",
CaptchaHiddenTokenName = "DNTCaptchaToken",
CaptchaInputName = "DNTCaptchaInputText"
}).Identifier("dntCaptcha");
});
services.AddControllers().AddJsonOptions(opt =>
{
// This is necessary for the languages enum.
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
void ConfigureLogging(ILoggingBuilder logging, IHostEnvironment env, IConfiguration configuration)
{
logging.ClearProviders();
logging.AddDebug();
if (env.IsDevelopment())
{
logging.AddConsole();
}
logging.AddConfiguration(configuration.GetSection("Logging"));
}
void ConfigureMiddlewares(IApplicationBuilder app, IHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(); // The Swagger UI can be found at https://localhost:<port>/swagger
}
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseRateLimiter();
app.UseAuthorization();
}
void ConfigureEndpoints(IApplicationBuilder app)
=> app.UseEndpoints(endpoints => { endpoints.MapControllers(); });