-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
228 lines (195 loc) · 7.53 KB
/
Copy pathProgram.cs
File metadata and controls
228 lines (195 loc) · 7.53 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Net.Http.Headers;
using System.IO.Compression;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Add response compression for better performance
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
{
"text/html",
"text/css",
"application/javascript",
"text/javascript",
"application/json",
"application/xml",
"text/xml",
"image/svg+xml",
"application/font-woff",
"application/font-woff2",
"font/woff",
"font/woff2"
});
});
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.SmallestSize;
});
// Add HTTP caching
builder.Services.AddResponseCaching();
// Add HSTS (HTTP Strict Transport Security)
builder.Services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(365);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
// Enforce HTTPS
app.UseHttpsRedirection();
// Enable response compression
app.UseResponseCompression();
// Enable response caching
app.UseResponseCaching();
// Custom content type provider for additional file types
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings[".webmanifest"] = "application/manifest+json";
contentTypeProvider.Mappings[".webp"] = "image/webp";
contentTypeProvider.Mappings[".avif"] = "image/avif";
contentTypeProvider.Mappings[".woff2"] = "font/woff2";
// Static files with optimized caching
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider,
OnPrepareResponse = ctx =>
{
var headers = ctx.Context.Response.Headers;
var contentType = ctx.Context.Response.ContentType ?? "";
// Different cache durations based on file type
int maxAge;
if (contentType.Contains("image/") || contentType.Contains("font/"))
{
// Images and fonts: 1 year (immutable assets)
maxAge = 60 * 60 * 24 * 365;
headers.Append(HeaderNames.CacheControl, $"public,max-age={maxAge},immutable");
}
else if (contentType.Contains("text/css") || contentType.Contains("javascript"))
{
// CSS and JS: 1 week (may change more frequently)
maxAge = 60 * 60 * 24 * 7;
headers.Append(HeaderNames.CacheControl, $"public,max-age={maxAge}");
}
else if (ctx.File.Name == "sitemap.xml" || ctx.File.Name == "robots.txt")
{
// SEO files: 1 day
maxAge = 60 * 60 * 24;
headers.Append(HeaderNames.CacheControl, $"public,max-age={maxAge}");
}
else
{
// Everything else: 1 month
maxAge = 60 * 60 * 24 * 30;
headers.Append(HeaderNames.CacheControl, $"public,max-age={maxAge}");
}
}
});
// Add security and SEO headers middleware
app.Use(async (context, next) =>
{
var headers = context.Response.Headers;
// Security headers
headers.Append("X-Content-Type-Options", "nosniff");
headers.Append("X-Frame-Options", "DENY");
headers.Append("X-XSS-Protection", "1; mode=block");
headers.Append("Referrer-Policy", "strict-origin-when-cross-origin");
headers.Append("Permissions-Policy", "geolocation=(), microphone=(), camera=(), payment=(), usb=()");
headers.Append("Cross-Origin-Embedder-Policy", "unsafe-none");
headers.Append("Cross-Origin-Opener-Policy", "same-origin");
headers.Append("Cross-Origin-Resource-Policy", "cross-origin");
// Content Security Policy (updated for Font Awesome CDN)
headers.Append("Content-Security-Policy",
"default-src 'self'; " +
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.tailwindcss.com https://cloud.umami.is https://www.googletagmanager.com https://www.google-analytics.com; " +
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdnjs.cloudflare.com; " +
"font-src 'self' https://fonts.gstatic.com https://cdnjs.cloudflare.com data:; " +
"img-src 'self' data: https: http:; " +
"connect-src 'self' https://cloud.umami.is https://www.google-analytics.com https://analytics.google.com; " +
"frame-ancestors 'none'; " +
"base-uri 'self'; " +
"form-action 'self';");
await next();
});
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
// SEO-friendly endpoints with proper headers
// Serve robots.txt with cache headers
app.MapGet("/robots.txt", async context =>
{
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=86400"); // 1 day
await context.Response.SendFileAsync("wwwroot/robots.txt");
});
// Serve sitemap.xml with cache headers
app.MapGet("/sitemap.xml", async context =>
{
context.Response.ContentType = "application/xml; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=86400"); // 1 day
await context.Response.SendFileAsync("wwwroot/sitemap.xml");
});
// Serve humans.txt
app.MapGet("/humans.txt", async context =>
{
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=604800"); // 1 week
await context.Response.SendFileAsync("wwwroot/humans.txt");
});
// Serve security.txt (RFC 9116 compliant location)
app.MapGet("/.well-known/security.txt", async context =>
{
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=604800"); // 1 week
await context.Response.SendFileAsync("wwwroot/.well-known/security.txt");
});
// Also serve security.txt from root for compatibility
app.MapGet("/security.txt", async context =>
{
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=604800"); // 1 week
await context.Response.SendFileAsync("wwwroot/.well-known/security.txt");
});
// Serve schema.json for structured data
app.MapGet("/schema.json", async context =>
{
context.Response.ContentType = "application/ld+json; charset=utf-8";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=86400"); // 1 day
if (File.Exists("wwwroot/schema.json"))
{
await context.Response.SendFileAsync("wwwroot/schema.json");
}
else
{
context.Response.StatusCode = 404;
}
});
// Favicon fallback for older browsers
app.MapGet("/favicon.ico", async context =>
{
context.Response.ContentType = "image/x-icon";
context.Response.Headers.Append(HeaderNames.CacheControl, "public,max-age=31536000,immutable"); // 1 year
if (File.Exists("wwwroot/favicon.ico"))
{
await context.Response.SendFileAsync("wwwroot/favicon.ico");
}
else
{
context.Response.StatusCode = 204; // No content
}
});
app.Run();