A practical, defensible guide for developers and security testers that explains the what, why, and how of penetration testing (pen-testing) for ASP.NET Core apps—with C# examples you can drop into tooling or test harnesses. Only test systems you own or have explicit written permission to test. Unauthorized testing is illegal.
1. What is penetration testing (short)
Penetration testing is a simulated attack against an application to discover security weaknesses before an attacker does. It complements static analysis (SAST) and dynamic scanning (DAST) by combining automated scanning with manual, creative testing of logic and flows.
Goals
Find authentication/authorization flaws and business logic issues.
Find input validation, injection, XSS, CSRF, file upload, and session weaknesses.
Measure impact (data exposure, privilege escalation, remote code execution).
Produce reproducible findings and remediation guidance.
2. Rules of engagement (must read)
Before testing
Get written authorization with scope, dates, allowed tools, and contacts.
Define in-scope assets (hostnames, endpoints, and environments). Prefer dedicated staging environments.
Define out-of-scope items (production databases, payment processors, third-party APIs).
Arrange backup and incident contacts in case of accidental disruption.
Keep tests non-destructive unless explicitly permitted.
3. Test methodology (high level)
Reconnaissance: gather info about app routes, public assets, and technologies.
Mapping & crawling: enumerate endpoints, API routes, and forms.
Automated scanning: run DAST scanners for common issues.
Manual testing: authentication/authorization logic, business logic flaws, injection, XSS, CSRF, and file upload attacks.
Exploitation & validation: validate findings with non-destructive proof-of-concept (PoC).
Reporting & remediation: document reproduction steps, impact, and mitigation.
4. Reconnaissance & mapping
What to gather:
Hostnames, subdomains, and exposed ports.
App tech (ASP.NET Core version, server headers, frameworks).
Public routes, sitemap, API endpoints, JS files.
Authentication flow and third-party integrations.
Tools (high-level): browser DevTools, automated site crawlers (OWASP ZAP / Burp Spider), robots.txt, sitemap.
C# reconnaissance snippet: fetch /.well-known and robots automatically.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public static async Task ReconAsync(string baseUrl)
{
using var client = new HttpClient { BaseAddress = new Uri(baseUrl) };
var endpoints = new[] { "/robots.txt", "/.well-known/security.txt", "/favicon.ico" };
foreach (var ep in endpoints)
{
var res = await client.GetAsync(ep);
Console.WriteLine($"{ep} -> {(int)res.StatusCode} {res.ReasonPhrase}");
}
}
5. Automated scanning (DAST)
Automated scanners quickly find low-hanging fruit:
OWASP ZAP
Burp Suite (Professional for automation)
Nikto (web server issues)
SecurityHeaders.io for header checks
Best practice
Scan the staging environment.
Use authenticated scans for deeper coverage (login scripts).
Review results—scanners produce false positives.
You can orchestrate ZAP in CI (simple Docker command):
docker run --rm owasp/zap2docker-stable zap-baseline.py -t https://staging.example.com -r zap_report.html
6. Manual testing focus areas (with C# testing helpers)
Below are the core manual tests and small C# examples to help automate safe checks.
6.1 Authentication & session management
Check
Weak password policy, account enumeration, session fixation, missing MFA.
Cookie flags:
HttpOnly,Secure,SameSite.Session timeout / logout behavior.
C#: test whether login sets secure cookie flags
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public static async Task CheckCookieFlags(HttpClient client, string loginUrl)
{
var res = await client.GetAsync(loginUrl);
if (res.Headers.TryGetValues("Set-Cookie", out var setCookies))
{
foreach (var c in setCookies)
{
Console.WriteLine(c);
if (!c.Contains("HttpOnly")) Console.WriteLine("Warning: Cookie missing HttpOnly");
if (!c.Contains("Secure")) Console.WriteLine("Warning: Cookie missing Secure");
}
}
}
6.2 Authorization & access control
Check
Horizontal privilege escalation (user A accessing user B resources).
Vertical privilege escalation (user role bypass to admin).
Insecure direct object references (IDOR).
C# approach: call API endpoints with different roles/tokens and assert 403 where appropriate.
// Simplified: send requests with different Authorization headers and assert response codes
6.3 Input validation & injection
Check
SQL/command injection, unsafe raw SQL, and deserialization issues.
Validate all inputs and ensure use of parameterized queries/ORM.
C# fuzzing snippet to POST common suspicious inputs to an endpoint:
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
public static async Task FuzzEndpoint(HttpClient client, string endpoint)
{
var payloads = new[] {
new { name = "normal" },
new { name = "' OR '1'='1" },
new { name = "<script>alert(1)</script>" },
new { name = new string('A', 5000) }
};
foreach (var p in payloads)
{
var res = await client.PostAsJsonAsync(endpoint, p);
Console.WriteLine($"Payload len {JsonSerializer.Serialize(p).Length} => {(int)res.StatusCode}");
}
}
Join the conversation! Your thoughts help the community grow.