1- using System ;
2- using System . Collections . Generic ;
3- using System . Threading . Tasks ;
4- using Microsoft . AspNetCore . Http ;
5- using Microsoft . Extensions . Logging ;
61using Microsoft . Extensions . Options ;
72
83namespace MyApp ;
@@ -41,22 +36,13 @@ public class UserAgentBlockingOptions
4136/// <summary>
4237/// Middleware that blocks requests from specific user agents
4338/// </summary>
44- public class UserAgentBlockingMiddleware
39+ public class UserAgentBlockingMiddleware (
40+ RequestDelegate next ,
41+ IOptions < UserAgentBlockingOptions > options ,
42+ ILogger < UserAgentBlockingMiddleware > logger )
4543{
46- private readonly RequestDelegate _next ;
47- private readonly UserAgentBlockingOptions _options ;
48- private readonly ILogger < UserAgentBlockingMiddleware > _logger ;
49-
50- public UserAgentBlockingMiddleware (
51- RequestDelegate next ,
52- IOptions < UserAgentBlockingOptions > options ,
53- ILogger < UserAgentBlockingMiddleware > logger )
54- {
55- _next = next ?? throw new ArgumentNullException ( nameof ( next ) ) ;
56- _options = options ? . Value ?? throw new ArgumentNullException ( nameof ( options ) ) ;
57- _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
58- }
59-
44+ UserAgentBlockingOptions Options => options ? . Value ?? throw new ArgumentNullException ( nameof ( options ) ) ;
45+
6046 public async Task InvokeAsync ( HttpContext context )
6147 {
6248 if ( context == null )
@@ -71,26 +57,26 @@ public async Task InvokeAsync(HttpContext context)
7157 if ( ShouldBlockUserAgent ( userAgent ) )
7258 {
7359 // Log the blocked request if enabled
74- if ( _options . LogBlockedRequests )
60+ if ( Options . LogBlockedRequests )
7561 {
76- _logger . LogInformation (
62+ logger . LogInformation (
7763 "Request blocked from user agent: {UserAgent}, IP: {IPAddress}, Path: {Path}" ,
7864 userAgent ,
7965 context . Connection . RemoteIpAddress ,
8066 context . Request . Path ) ;
8167 }
8268
8369 // Set the response status code
84- context . Response . StatusCode = _options . BlockedStatusCode ;
70+ context . Response . StatusCode = Options . BlockedStatusCode ;
8571 context . Response . ContentType = "text/plain" ;
8672
8773 // Write the blocked message to the response
88- await context . Response . WriteAsync ( _options . BlockedMessage ) ;
74+ await context . Response . WriteAsync ( Options . BlockedMessage ) ;
8975 return ;
9076 }
9177
9278 // If not blocked, continue to the next middleware
93- await _next ( context ) ;
79+ await next ( context ) ;
9480 }
9581
9682 private bool ShouldBlockUserAgent ( string userAgent )
@@ -102,9 +88,9 @@ private bool ShouldBlockUserAgent(string userAgent)
10288 return false ;
10389 }
10490
105- foreach ( var blockedAgent in _options . BlockedUserAgents )
91+ foreach ( var blockedAgent in Options . BlockedUserAgents )
10692 {
107- var comparison = _options . IgnoreCase
93+ var comparison = Options . IgnoreCase
10894 ? StringComparison . OrdinalIgnoreCase
10995 : StringComparison . Ordinal ;
11096
0 commit comments