Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (112 loc) · 4.66 KB
/
Copy pathProgram.cs
File metadata and controls
138 lines (112 loc) · 4.66 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using InventoryApi.Data;
using InventoryApi.Middleware;
using InventoryApi.Models;
using InventoryApi.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddControllers();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularDev", policy =>
{
policy.WithOrigins(
"http://localhost:4200",
"https://happy-pond-0aab59f00.3.azurestaticapps.net")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddDbContext<InventoryDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("InventoryDb"),
sqlOptions => sqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(10),
errorNumbersToAdd: null)));
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddSingleton<IPasswordHasher<User>, PasswordHasher<User>>();
var jwtSigningKey = builder.Configuration["Jwt:SigningKey"]
?? throw new InvalidOperationException("Jwt:SigningKey is not configured.");
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(jwtSigningKey)),
ClockSkew = TimeSpan.FromMinutes(1),
};
});
builder.Services.AddAuthorization();
// On Azure App Service every request arrives through Azure's front end, so without this the
// app sees the front end's IP instead of the visitor's (and http instead of https).
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// The front end has no fixed address to allow-list, so accept the headers from any proxy.
// Safe because the app is only reachable through that front end, and ForwardLimit (default 1)
// only takes the last X-Forwarded-For entry - the one Azure appended - so a value the
// client sent itself is ignored.
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
});
builder.Services.AddAuthRateLimiting();
// Deliberately no database check: /health must answer instantly even while the serverless
// database is paused, so it only proves the app itself is up.
builder.Services.AddHealthChecks();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
try
{
var context = scope.ServiceProvider.GetRequiredService<InventoryDbContext>();
var passwordHasher = scope.ServiceProvider.GetRequiredService<IPasswordHasher<User>>();
DemoDataSeeder.SeedDemoUser(context, passwordHasher);
ProductDataSeeder.SeedProducts(context);
}
catch (Exception ex)
{
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
logger.LogWarning(ex, "Failed to seed demo data at startup - will retry on next restart.");
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
// First, so everything after it (rate limiting, logging, HTTPS redirection) sees the real client.
app.UseForwardedHeaders();
app.UseExceptionHandler();
if (!app.Environment.IsDevelopment())
{
// Tells browsers to use HTTPS only for this host; sent on HTTPS responses only, which
// UseForwardedHeaders above makes the app recognise on Azure.
app.UseHsts();
}
app.UseApiSecurityHeaders();
app.UseHttpsRedirection();
app.UseCors("AllowAngularDev");
// After CORS, so a 429 still carries the CORS headers - otherwise the browser hides the
// status from the frontend and it looks like a network failure.
app.UseRateLimiter();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/health");
app.Run();