Posted 11/15/2024
Its always better to show headers which hold information about server, framework or language to hide the information from attacker.
builder.WebHost.ConfigureKestrel(host => { host.AddServerHeader = false; });
Always use Strict Transport Security Protocol and HTTPS Redirection in production apps:
var builder = WebApplication.CreateBuilder(args);
...
var app = builder.Build();
...
app.UseHsts();
app.UseHttpsRedirection();
using Microsoft.Extensions.Primitives;
app.Use(async (context, next) =>
{
context.Response.Headers.Add("Content-Security-Policy", new StringValues("default-src 'self'"));
context.Response.Headers.Add("X-Content-Type-Options", new StringValues("nosniff"));
context.Response.Headers.Add("X-Frame-Options", new StringValues("SAMEORIGIN"));
context.Response.Headers.Add("X-XSS-Protection", new StringValues("1; mode=block"));
await next();
});