Hardening for ASP.NET Cores
ASP.NET Core hardening means writing code and configuration that resist common attacks. It includes validating input, preventing SQL injection through parameterised queries, protecting against XSS and CSRF, enforcing HTTPS and security headers, managing secrets outside source code and applying rate limiting to reduce abuse.
Stage: Security · Part of the ASP.NET Core roadmap · Last reviewed 19 November 2026
What to learn in Hardening
- Prevent SQL injection by using EF Core or parameterised queries
- Enable HTTPS redirection and HSTS in production
- Use anti-forgery tokens to defend cookie-based forms against CSRF
- Keep secrets in user secrets or a vault, not in appsettings
- Add rate limiting middleware to a public endpoint
Hardening interview questions
How do you prevent SQL injection in .NET?
Never build SQL by concatenating user input. Use EF Core LINQ queries, which parameterise automatically, or pass values as parameters in raw SQL through FromSql with interpolated arguments or DbParameter. The database then treats input as data, not as executable SQL.
Where should connection strings and API keys be kept?
Not in source control. In development use the Secret Manager tool, and in production use environment variables or a secret store such as Azure Key Vault. ASP.NET Core configuration merges these sources, so code reads them the same way.
Where Hardening fits in the ASP.NET Core roadmap
Hardening belongs to the Security stage (intermediate level), which takes about 21 hours. Protect it.
Other units in this stage
Back to the full ASP.NET Core roadmap