Secure Coding: HTTPS, Secrets and Common Attacks

Dot NET Programming 15 min min read Updated: Nov 16, 2026 Advanced
Secure Coding: HTTPS, Secrets and Common Attacks
Advanced Topic 4 of 4

Security Is a Habit

Most hacks use simple, known mistakes. If you avoid the ones below, your application is safer than most.

1. SQL Injection

csharp // BAD: user input glued into SQL var sql = "SELECT * FROM Users WHERE Name = '" + name + "'"; // name = ' OR 1=1 -- // GOOD: EF Core LINQ (always parameterized) var users = await db.Users.Where(u => u.Name == name).ToListAsync(); // GOOD: raw SQL with interpolation is turned into parameters by FromSql var users2 = await db.Users.FromSql($"SELECT * FROM Users WHERE Name = {name}").ToListAsync();
Never: Use FromSqlRaw or SqlCommand with string concatenation of user input.

2. Cross-Site Scripting (XSS)

XSS happens when an attacker's text runs as JavaScript in another user's browser. Razor and Blazor encode output by default.

html <!-- Safe: encoded automatically --> <p>@Model.Comment</p> <!-- Dangerous: only for trusted HTML --> <p>@Html.Raw(Model.Comment)</p>

3. CSRF

A malicious site makes the browser send a request to your site using the user's cookies. Cookie-based forms need an antiforgery token. Razor Pages and MVC forms include it automatically. APIs that use Authorization: Bearer headers are not affected by CSRF because browsers do not attach those headers by themselves.

csharp builder.Services.AddAntiforgery(); app.UseAntiforgery();

4. Broken Access Control

The number one risk. Always check who the user is and that the data belongs to them.

csharp var order = await db.Orders .Where(o => o.Id == id && o.OwnerId == currentUserId) // filter by owner .FirstOrDefaultAsync();

5. HTTPS and HSTS

csharp if (!app.Environment.IsDevelopment()) app.UseHsts(); // tell browsers: always use HTTPS for this site app.UseHttpsRedirection();

6. Secrets Management

WhereSafe?
In source code or appsettings.json in gitNo
User secrets (development)Yes, local only
Environment variablesYes
Azure Key Vault, AWS Secrets Manager, HashiCorp VaultBest for production

7. Validate Input and Limit Size

csharp builder.WebHost.ConfigureKestrel(k => k.Limits.MaxRequestBodySize = 5 * 1024 * 1024); // validate file uploads: type, size, never trust the file name var safeName = Guid.NewGuid() + Path.GetExtension(file.FileName);

8. Security Headers

csharp app.Use(async (ctx, next) => { ctx.Response.Headers["X-Content-Type-Options"] = "nosniff"; ctx.Response.Headers["X-Frame-Options"] = "DENY"; ctx.Response.Headers["Referrer-Policy"] = "no-referrer"; ctx.Response.Headers["Content-Security-Policy"] = "default-src 'self'"; await next(); });

9. Protect Data at Rest

csharp builder.Services.AddDataProtection(); public class TokenProtector(IDataProtectionProvider provider) { private readonly IDataProtector _p = provider.CreateProtector("Tokens.v1"); public string Protect(string text) => _p.Protect(text); public string Unprotect(string text) => _p.Unprotect(text); }

10. Keep Dependencies Updated

bash dotnet list package --vulnerable --include-transitive dotnet list package --outdated
Quick checklist: Parameterized queries, encoded output, HTTPS everywhere, ownership checks, no secrets in git, validated input, updated packages, safe error messages.

Conclusion

Security means avoiding a short list of known mistakes. Use EF Core parameters, keep Razor encoding, check ownership, force HTTPS, store secrets outside code and update packages regularly.

Quick Summary: SQL injection, XSS, CSRF, access control, HTTPS, secrets, validation, updates.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators