No application is 100% immune to security threats. Despite best practices in secure coding, attackers may still exploit vulnerabilities, misconfigurations, or third-party dependencies. That’s why every ASP.NET Core application should have a well-defined Incident Response Plan (IRP) to quickly detect, respond to, and recover from security breaches.

This article walks you through the steps, tools, and sample code for building an incident response strategy specifically tailored for ASP.NET Core apps.

What Is an Incident Response Plan?

An Incident Response Plan (IRP) is a documented, structured approach to handling security breaches. Its purpose is to:

Key Phases of Incident Response in ASP.NET Core

1. Preparation

Before a breach happens, ensure:

Example: Enable structured security logging with Serilog or Application Insights:

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("logs/security-.log", rollingInterval: RollingInterval.Day)
    .CreateLogger();

2. Identification

Detect and confirm the breach.
Common signs in ASP.NET Core apps include:

Example: Log and flag repeated failed logins:

_logger.LogWarning("Failed login attempt {Count} for {Username} from {IP}",
                   attemptCount, username, ip);
if (attemptCount > 5)
{
    _logger.LogError("Possible brute-force attack detected from IP {IP}", ip);
    // Trigger an alert or block IP temporarily
}

3. Containment

Limit the scope of the breach while keeping services running.

Example: Blocking an IP using ASP.NET Core middleware:

public class IpBlockMiddleware
{
    private readonly RequestDelegate _next;
    private static readonly HashSet<string> BlockedIps = new();

    public IpBlockMiddleware(RequestDelegate next) => _next = next;

    public async Task Invoke(HttpContext context)
    {
        var ip = context.Connection.RemoteIpAddress?.ToString();
        if (BlockedIps.Contains(ip))
        {
            context.Response.StatusCode = 403;
            await context.Response.WriteAsync("Access denied.");
            return;
        }

        await _next(context);
    }

    public static void BlockIp(string ip) => BlockedIps.Add(ip);
}

4. Eradication

Remove the root cause of the breach.

Use OWASP Dependency Check or dotnet list package --outdated to identify vulnerabilities.

5. Recovery

Restore services to normal operations.

6. Lessons Learned

After the incident, perform a post-mortem analysis:

Document findings and improve the incident response playbook.

Automating Incident Response in ASP.NET Core

You can integrate automated workflows for faster response:

Example: Global exception logging with IExceptionFilter:

public class SecurityExceptionFilter : IExceptionFilter
{
    private readonly ILogger<SecurityExceptionFilter> _logger;

    public SecurityExceptionFilter(ILogger<SecurityExceptionFilter> logger)
    {
        _logger = logger;
    }

    public void OnException(ExceptionContext context)
    {
        _logger.LogError(context.Exception,
                         "Security exception at {Path}", 
                         context.HttpContext.Request.Path);
    }
}

Incident Response Checklist for ASP.NET Core Apps

Conclusion

A well-prepared incident response plan ensures that your ASP.NET Core applications can withstand breaches and recover with minimal damage. By combining proactive monitoring, structured logging, automated containment, and post-incident learning, your development and security teams can respond to threats effectively.