Security Best Practices in ASP.NET Core
π‘ Concept Name
Security Best Practices in ASP.NET Core
π Quick Intro
Security is a critical aspect of web development. ASP.NET Core offers built-in features like HTTPS redirection, HSTS headers, and CSP to ensure apps are safe from common threats like XSS, man-in-the-middle attacks, and protocol downgrade attacks.
π§ Analogy / Short Story
Think of your web app like a house. HTTPS is like installing secure, tamper-proof locks. HSTS is a rule you set that says, "Never enter my house without locking the door." CSP is like only allowing trusted guests (scripts, images) inside while blocking strangers.
π§ Technical Explanation
- HTTPS Redirection: Forces all HTTP traffic to use secure HTTPS. Done via `app.UseHttpsRedirection()` in `Startup.cs`.
- HSTS (HTTP Strict Transport Security): Tells browsers to never use HTTP for future requests. Implemented via `app.UseHsts()` middleware.
- CSP (Content Security Policy): Prevents unwanted scripts/styles from executing by controlling allowed sources through response headers.
π― Purpose & Use Case
- β Enforce encrypted communication
- β Prevent downgrade and MITM attacks
- β Mitigate XSS with CSP headers
- β Secure APIs and client-side scripts
- β Build trust for users accessing critical data
π» Real Code Example
Startup.cs Middleware Configuration:
if (!app.Environment.IsDevelopment())
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.Use(async (context, next) =>
{
context.Response.Headers.Add("Content-Security-Policy", "default-src 'self'");
await next();
});
Highlight: This setup forces HTTPS, applies HSTS, and sets a basic CSP headerβall critical for production security.

β Interview Q&A
Q1: What does HTTPS protect against?
A: Eavesdropping, tampering, and man-in-the-middle attacks.
Q2: How do you enforce HTTPS in ASP.NET Core?
A: Use `app.UseHttpsRedirection()` middleware.
Q3: What is HSTS and how is it used?
A: HSTS forces browsers to use HTTPS for future visits; use `app.UseHsts()`.
Q4: Can HSTS be used in development?
A: No, it's recommended only for production.
Q5: What is the risk of not using CSP headers?
A: Your app becomes vulnerable to XSS attacks.
Q6: How is CSP implemented in ASP.NET Core?
A: By adding `Content-Security-Policy` headers to the response.
Q7: How to remove mixed content issues?
A: Ensure all resources (scripts/images) are loaded over HTTPS.
Q8: What happens if you don't use HTTPS for login pages?
A: User credentials can be stolen via sniffing.
Q9: Is HTTPS enough for securing modern web apps?
A: No, use in combination with CSP, HSTS, and authentication mechanisms.
Q10: How do browsers know to use HTTPS due to HSTS?
A: They remember the policy sent by the server via HSTS header.
π MCQs
-
Which middleware enforces HTTPS in ASP.NET Core?
- UseAuthentication()
- UseEndpoints()
- UseHttpsRedirection() βοΈ
- UseRouting()
-
What does HSTS stand for?
- HTTP Security Token Service
- High Security Transport Service
- HTTP Strict Transport Security βοΈ
- Hybrid Secure Traffic Solution
-
What is the purpose of CSP?
- To cache static files
- To manage configuration
- To control allowed content sources βοΈ
- To redirect requests
-
Where is the CSP header added?
- Startup.cs
- HTML meta tags
- Response headers βοΈ
- Controller
-
What attack does HTTPS protect against?
- SQL Injection
- XSS
- Man-in-the-middle βοΈ
- DDOS
-
Which header signals HTTPS enforcement to browsers?
- Cache-Control
- Strict-Transport-Security βοΈ
- Authorization
- Secure-Connection
-
Why avoid HSTS in development?
- Slows app
- Conflicts with sessions
- Breaks localhost HTTPS βοΈ
- Consumes memory
-
What value of CSP allows scripts only from the same origin?
- default-src 'all'
- default-src 'self' βοΈ
- script-src 'trusted'
- style-src 'none'
-
How does HSTS improve security?
- Prevents cross-origin requests
- Forces HTTPS βοΈ
- Hashes passwords
- Blocks all headers
-
What's a good combination for frontend security?
- HTTPS, HSTS, CSP βοΈ
- HTTPS, Cookies, Sessions
- Routing, SSL, Auth
- Middleware, DB, Sessions
π‘ Bonus Insight
Security is not a one-time task. Keep dependencies up-to-date, validate input, log incidents, and adopt a zero-trust mindset. Use `dotnet-outdated`, `OWASP ZAP`, and dependency scanning tools regularly.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!