|
14 | 14 | namespace Dotnet.Samples.AspNetCore.WebApi.Extensions;
|
15 | 15 |
|
16 | 16 | /// <summary>
|
17 |
| -/// Extension methods for WebApplicationBuilder to encapsulate service configuration. |
| 17 | +/// Extension methods for IServiceCollection to encapsulate service configuration. |
18 | 18 | /// </summary>
|
19 |
| -public static class ServiceCollectionExtensions |
| 19 | +public static partial class ServiceCollectionExtensions |
20 | 20 | {
|
21 | 21 | /// <summary>
|
22 | 22 | /// Adds DbContextPool with SQLite configuration for PlayerDbContext.
|
@@ -54,16 +54,25 @@ IWebHostEnvironment environment
|
54 | 54 | /// <see href="https://learn.microsoft.com/en-us/aspnet/core/security/cors"/>
|
55 | 55 | /// </summary>
|
56 | 56 | /// <param name="services">The IServiceCollection instance.</param>
|
| 57 | + /// <param name="environment">The web host environment.</param> |
57 | 58 | /// <returns>The IServiceCollection for method chaining.</returns>
|
58 |
| - public static IServiceCollection AddCorsDefaultPolicy(this IServiceCollection services) |
| 59 | + public static IServiceCollection AddCorsDefaultPolicy( |
| 60 | + this IServiceCollection services, |
| 61 | + IWebHostEnvironment environment |
| 62 | + ) |
59 | 63 | {
|
60 |
| - services.AddCors(options => |
| 64 | + if (environment.IsDevelopment()) |
61 | 65 | {
|
62 |
| - options.AddDefaultPolicy(corsBuilder => |
| 66 | + services.AddCors(options => |
63 | 67 | {
|
64 |
| - corsBuilder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); |
| 68 | + options.AddDefaultPolicy(corsBuilder => |
| 69 | + { |
| 70 | + corsBuilder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); |
| 71 | + }); |
65 | 72 | });
|
66 |
| - }); |
| 73 | + } |
| 74 | + |
| 75 | + // No CORS configured in Production or other environments |
67 | 76 |
|
68 | 77 | return services;
|
69 | 78 | }
|
@@ -143,4 +152,39 @@ public static IServiceCollection RegisterPlayerRepository(this IServiceCollectio
|
143 | 152 | services.AddScoped<IPlayerRepository, PlayerRepository>();
|
144 | 153 | return services;
|
145 | 154 | }
|
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Adds rate limiting configuration with IP-based partitioning. |
| 158 | + /// <br /> |
| 159 | + /// <see href="https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit"/> |
| 160 | + /// </summary> |
| 161 | + /// <param name="services">The IServiceCollection instance.</param> |
| 162 | + /// <returns>The IServiceCollection for method chaining.</returns> |
| 163 | + public static IServiceCollection AddFixedWindowRateLimiter(this IServiceCollection services) |
| 164 | + { |
| 165 | + services.AddRateLimiter(options => |
| 166 | + { |
| 167 | + options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>( |
| 168 | + httpContext => |
| 169 | + { |
| 170 | + var partitionKey = HttpContextUtilities.ExtractIpAddress(httpContext); |
| 171 | + |
| 172 | + return RateLimitPartition.GetFixedWindowLimiter( |
| 173 | + partitionKey: partitionKey, |
| 174 | + factory: _ => new FixedWindowRateLimiterOptions |
| 175 | + { |
| 176 | + PermitLimit = 60, |
| 177 | + Window = TimeSpan.FromSeconds(60), |
| 178 | + QueueProcessingOrder = QueueProcessingOrder.OldestFirst, |
| 179 | + QueueLimit = 0 |
| 180 | + } |
| 181 | + ); |
| 182 | + } |
| 183 | + ); |
| 184 | + |
| 185 | + options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| 186 | + }); |
| 187 | + |
| 188 | + return services; |
| 189 | + } |
146 | 190 | }
|
0 commit comments