-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRateLimitingMiddleWare.cs
More file actions
75 lines (65 loc) · 2.43 KB
/
Copy pathRateLimitingMiddleWare.cs
File metadata and controls
75 lines (65 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections.Concurrent;
namespace AdminAuth.Middleware;
/// <summary>
/// Tracks failed login attempts per IP and blocks after threshold.
/// </summary>
public class LoginRateLimitMiddleware
{
private readonly RequestDelegate _next;
private readonly int _maxAttempts;
private readonly TimeSpan _window;
// Thread-safe store: IP → (attempt count, window start)
private static readonly ConcurrentDictionary<string, (int Count, DateTime WindowStart)> _attempts = new();
public LoginRateLimitMiddleware(RequestDelegate next, IConfiguration config)
{
_next = next;
_maxAttempts = config.GetValue<int>("RateLimit:MaxLoginAttempts", 5);
_window = TimeSpan.FromMinutes(config.GetValue<int>("RateLimit:WindowMinutes", 10));
}
public async Task InvokeAsync(HttpContext context)
{
// Only gate POST /auth/login
if (context.Request.Path.StartsWithSegments("/auth/login") &&
context.Request.Method == "POST")
{
var ip = GetClientIp(context);
if (IsBlocked(ip))
{
context.Response.StatusCode = 429; // Too Many Requests
await context.Response.WriteAsync("Too many login attempts. Please wait and try again.");
return;
}
}
await _next(context);
}
public static void RecordFailedAttempt(string ip)
{
_attempts.AddOrUpdate(ip,
addValue: (1, DateTime.UtcNow),
updateValueFactory: (_, existing) =>
{
var (count, start) = existing;
// Reset window if expired
if (DateTime.UtcNow - start > TimeSpan.FromMinutes(10))
return (1, DateTime.UtcNow);
return (count + 1, start);
});
}
public static void ClearAttempts(string ip) => _attempts.TryRemove(ip, out _);
private bool IsBlocked(string ip)
{
if (!_attempts.TryGetValue(ip, out var entry)) return false;
if (DateTime.UtcNow - entry.WindowStart > _window)
{
_attempts.TryRemove(ip, out _);
return false;
}
return entry.Count >= _maxAttempts;
}
public static string GetClientIp(HttpContext context)
{
return context.Request.Headers["X-Forwarded-For"].FirstOrDefault()
?? context.Connection.RemoteIpAddress?.ToString()
?? "unknown";
}
}