Introduction
In today’s world of cyber threats and data breaches, security is no longer optional—it’s a necessity. As developers, we are responsible for ensuring that our applications are built on secure coding practices.
C# applications, whether built on ASP.NET Core, .NET Framework MVC, Web APIs, or desktop apps, are common targets for attackers. Vulnerabilities such as SQL Injection, Cross-Site Scripting (XSS), Cross-Site Request Forgery (CSRF), broken authentication, insecure file handling, and misconfigured security settings can compromise user data and business reputation.
This article provides a complete application security checklist for C# developers . You can use this as a practical guide to make sure your applications follow the best security practices from design to deployment .
1. Authentication and Authorization
Authentication and authorization are the first line of defense.
Use ASP.NET Identity, OAuth2.0, or OpenID Connect instead of custom authentication logic.
Always hash and salt passwords with PBKDF2, bcrypt, or Argon2.
Enforce multi-factor authentication (MFA) for critical systems.
Implement Role-Based Access Control (RBAC) or Claims-Based Authorization.
[Authorize(Roles = "Admin")]
public IActionResult AdminDashboard()
{
return View();
}
2. Secure Data Access (Prevent SQL Injection)
SQL injection remains one of the most dangerous attacks .
Safe Example (Entity Framework LINQ)
var user = db.Users.FirstOrDefault(u => u.Username == username);
Safe Example (ADO.NET with parameters)
var cmd = new SqlCommand("SELECT * FROM Users WHERE Username = @username", conn);
cmd.Parameters.AddWithValue("@username", username);
Vulnerable Example
var query = $"SELECT * FROM Users WHERE Username = '{username}'";
Checklist : Always use parameterized queries or LINQ . Never concatenate user input.
3. Input Validation and Output Encoding
Unvalidated input can lead to XSS, injection, and logic flaws.
Use Data Annotations or FluentValidation.
Apply whitelisting where possible (e.g., regex for emails and numbers).
Always encode output to prevent XSS.
@Html.DisplayFor(model => model.Comment) // Razor auto-encodes
4. Secure Configuration
Application configuration often exposes sensitive data if not handled properly.
Never hardcode secrets in code. Use Azure Key Vault, AWS Secrets Manager, or environment variables.
Disable detailed error messages in production.
Enforce HTTPS/TLS 1.2+ for all requests.
app.UseHttpsRedirection();
Restrict configuration files like
web.configandappsettings.jsonfrom being exposed.
5. Web Security (CSRF, CORS, XSS)
Cross-Site Request Forgery (CSRF)
@Html.AntiForgeryToken()
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult SubmitForm(MyModel model)
{
// Safe form submission
}
Cross-Origin Resource Sharing (CORS)
app.UseCors(builder =>
builder.WithOrigins("https://trusteddomain.com")
.AllowAnyHeader()
.AllowAnyMethod());
Cookie Security
Mark cookies as
Secure,HttpOnly, andSameSite=Strict.
6. Logging and Monitoring
Logging is essential for detecting and responding to attacks.
Use structured logging frameworks like Serilog, NLog, or Microsoft.Extensions. Logging.
Never log sensitive data (passwords, tokens, credit card info).
Integrate monitoring with Application Insights, Splunk, or ELK Stack.
Join the conversation! Your thoughts help the community grow.