|
| 1 | +namespace LearningHub.Nhs.WebUI.Middleware |
| 2 | +{ |
| 3 | + using System.Threading.Tasks; |
| 4 | + using AspNetCoreRateLimit; |
| 5 | + using Microsoft.AspNetCore.Http; |
| 6 | + using Microsoft.Extensions.Logging; |
| 7 | + using Microsoft.Extensions.Options; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Defines the <see cref="LHIPRateLimitMiddleware" />. |
| 11 | + /// </summary> |
| 12 | + public class LHIPRateLimitMiddleware : IpRateLimitMiddleware |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Initializes a new instance of the <see cref="LHIPRateLimitMiddleware"/> class. |
| 16 | + /// </summary> |
| 17 | + /// <param name="next">The next.</param> |
| 18 | + /// <param name="processingStrategy">The processingStrategy.</param> |
| 19 | + /// <param name="options">The options.</param> |
| 20 | + /// <param name="policyStore">The policyStore.</param> |
| 21 | + /// <param name="config">The config.</param> |
| 22 | + /// <param name="logger">The logger.</param> |
| 23 | + public LHIPRateLimitMiddleware( |
| 24 | + RequestDelegate next, |
| 25 | + IProcessingStrategy processingStrategy, |
| 26 | + IOptions<IpRateLimitOptions> options, |
| 27 | + IIpPolicyStore policyStore, |
| 28 | + IRateLimitConfiguration config, |
| 29 | + ILogger<IpRateLimitMiddleware> logger) |
| 30 | + : base( |
| 31 | + next, |
| 32 | + processingStrategy, |
| 33 | + options, |
| 34 | + policyStore, |
| 35 | + config, |
| 36 | + logger) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The ReturnQuotaExceededResponse method. |
| 42 | + /// </summary> |
| 43 | + /// <param name="httpContext">The httpContext.</param> |
| 44 | + /// <param name="rule">The rule.</param> |
| 45 | + /// <param name="retryAfter">The retryAfter.</param> |
| 46 | + /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
| 47 | + public override Task ReturnQuotaExceededResponse( |
| 48 | + HttpContext httpContext, |
| 49 | + RateLimitRule rule, |
| 50 | + string retryAfter) |
| 51 | + { |
| 52 | + httpContext.Response.Headers["Location"] = "/TooManyRequests"; |
| 53 | + httpContext.Response.StatusCode = 302; |
| 54 | + return httpContext.Response.WriteAsync(string.Empty); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments