ASP.NET Core触发速率限制时页面重定向功能失效问题求助
ASP.NET Core触发速率限制时页面重定向功能失效问题求助
各位好,我正在基于ASP.NET Core实现固定窗口模式的速率限制功能,目前已经完成了基础的限流配置,代码如下:
builder.Services.AddRateLimiter(options => { options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => { var remoteIpAddress = httpContext.Connection.RemoteIpAddress; var localIpAddress = httpContext.Connection.LocalIpAddress; var partitionKey = $"{remoteIpAddress} {localIpAddress}"; return RateLimitPartition.GetFixedWindowLimiter(partitionKey, options => new FixedWindowRateLimiterOptions { AutoReplenishment = true, PermitLimit = 10, QueueLimit = 0, Window = TimeSpan.FromMinutes(1), QueueProcessingOrder = QueueProcessingOrder.OldestFirst }); }); options.OnRejected = async (context, cancellationToken) => { // 这里尝试做重定向,但完全没生效 context.HttpContext.Response.Redirect("/TooManyRequests"); await context.HttpContext.Response.CompleteAsync(cancellationToken); }; });
现在遇到一个头疼的问题:当触发速率限制时,我想把用户重定向到自定义的限流提示页面,但无论我在OnRejected回调里怎么写重定向逻辑,浏览器始终返回默认的429状态码,重定向完全没生效。我试过直接调用Response.Redirect(),也手动设置过302状态码和Location响应头,甚至尝试提前完成响应,但都没有效果。
有没有大佬遇到过类似的情况?或者知道怎么正确实现触发限流后的页面重定向?麻烦指点一下,感激不尽!
备注:内容来源于stack exchange,提问作者Ricardo Peres
相关产品推荐
相关产品推荐

