-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (94 loc) · 3.25 KB
/
Program.cs
File metadata and controls
114 lines (94 loc) · 3.25 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
using BitMap.SOCAPI.BusinessLogic;
using BitMap.SOCAPI.Data.Factories;
using BitMap.SOCAPI.Security;
using BitMap.SOCAPI.Services;
using BitMap.SOCAPI.Services.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Events;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
var jwtSection = builder.Configuration.GetSection("Jwt");
var jwtKey = jwtSection["Key"];
var externalConfigFile = builder.Configuration["ExternalConfig:BitMapConfigFile"];
if (!string.IsNullOrEmpty(externalConfigFile))
{
builder.Configuration.AddJsonFile(externalConfigFile, optional: false, reloadOnChange: true);
}
// Configure Serilog
var logPath = builder.Configuration["Serilog:WriteTo:0:Args:path"];
var logSizeLimit = builder.Configuration["Serilog:WriteTo:0:Args:fileSizeLimitBytes"];
Directory.CreateDirectory(Path.GetDirectoryName(logPath)!);
// Convert to long
long fileSizeLimitBytes = 5242880; // default fallback 5 MB
if (!string.IsNullOrEmpty(logSizeLimit) && long.TryParse(logSizeLimit, out var result))
{
fileSizeLimitBytes = result;
}
Directory.CreateDirectory(Path.GetDirectoryName(logPath)!);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.File(
path: logPath,
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: fileSizeLimitBytes,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 30,
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
)
.CreateLogger();
builder.Host.UseSerilog();
// Services
builder.Services.AddControllers();
builder.Services.AddOpenApi();
builder.Services.AddSingleton<CertificateProvider>();
builder.Services.AddScoped<CryptoService>();
builder.Services.AddSingleton<DataProviderFactory>();
builder.Services.AddScoped<IAppService, AppService>();
builder.Services.AddScoped<NavigationBusinessLogic>();
//builder.Services.AddAuthentication("Windows");
builder.Services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSection["Issuer"],
ValidAudience = jwtSection["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(jwtKey!)
)
};
});
var app = builder.Build();
// Middleware
app.UseAuthentication();
app.UseAuthorization();
// Dev tools
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapGet("/", () =>
Results.Content(
"<html><body style='font-family:Segoe UI; background:#fff;'>" +
"</body></html>",
"text/html"
)
);
// Controllers
app.MapControllers();
app.Run();