wasAsked.

Design a rate-limiting strategy for login, search, and authenticated write endpoints in ASP.NET Core.

CRED · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, ASP.NET Core, Authentication & Security

Community answers

Posted by wasAsked editorial collection

Rate-limit by a meaningful server-derived partition such as authenticated account, tenant, API key, and sometimes IP. Fixed windows are simple; token buckets allow controlled bursts; concurrency limits protect expensive operations. Example: builder.Services.AddRateLimiter(options => options.AddTokenBucketLimiter("writes", o => { o.TokenLimit = 20; o.TokensPerPeriod = 10; o.ReplenishmentPeriod = TimeSpan.FromSeconds(10); o.QueueLimit = 0; o.AutoReplenishment = true; })); Return 429 with Retry-After, apply stricter limits to login/password reset, and coordinate state across instances when required. Do not trust spoofable headers unless a trusted proxy rewrites them. Monitor rejects and combine throttling with authentication, quotas, input bounds, and upstream protection.

Backbencher Developer · · 0 likes

Different endpoints need different limits, because the threat and the normal usage differ. Login - Goal: stop credential stuffing and brute force. - Limit per IP and per account (for example 5 attempts per minute per username, with a longer lockout or exponential backoff after repeated failures). - Add CAPTCHA after several failures, and alert on unusual spikes. Return the same error for a wrong username or password so accounts cannot be enumerated. Search - Goal: protect expensive queries and scraping while keeping the UX smooth. - Token bucket per user or IP, allowing short bursts (typing) but capping sustained rates, e.g. 30 requests per 10 seconds. - Debounce on the client and cache popular queries on the server. Authenticated writes (create order, post comment) - Limit per user ID rather than IP, since many users can share one IP (offices, mobile networks). - Lower limits for costly or abusable actions (sending emails, invitations). Implementation in ASP.NET Core (.NET 7+): builder.Services.AddRateLimiter(o => { o.RejectionStatusCode = 429; o.AddPolicy("login", ctx => RateLimitPartition.GetFixedWindowLimiter( ctx.Connection.RemoteIpAddress?.ToString() ?? "unknown", _ => new FixedWindowRateLimiterOptions { PermitLimit = 5, Window = TimeSpan.FromMinutes(1) })); o.AddPolicy("writes", ctx => RateLimitPartition.GetTokenBucketLimiter( ctx.User.FindFirst("sub")?.Value ?? "anon", _ => new TokenBucketRateLimiterOptions { TokenLimit = 20, TokensPerPeriod = 10, ReplenishmentPeriod = TimeSpan.FromSeconds(10) })); }); app.UseRateLimiter(); Then apply [EnableRateLimiting("login")] or RequireRateLimiting("writes") on endpoints. Production considerations: - The built-in limiter is per process. With several instances, use a distributed store (Redis) or enforce limits at the gateway or WAF (Azure Front Door, Cloudflare, API Management). - Behind a proxy, configure forwarded headers correctly so you limit the real client IP. - Return 429 with a Retry-After header, log rejections and monitor them. - Exempt or separately limit health checks and trusted internal callers.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics