1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Threading . Tasks ;
4
- using Microsoft . AspNetCore . Http ;
5
- using Microsoft . Extensions . Logging ;
6
1
using Microsoft . Extensions . Options ;
7
2
8
3
namespace MyApp ;
@@ -41,22 +36,13 @@ public class UserAgentBlockingOptions
41
36
/// <summary>
42
37
/// Middleware that blocks requests from specific user agents
43
38
/// </summary>
44
- public class UserAgentBlockingMiddleware
39
+ public class UserAgentBlockingMiddleware (
40
+ RequestDelegate next ,
41
+ IOptions < UserAgentBlockingOptions > options ,
42
+ ILogger < UserAgentBlockingMiddleware > logger )
45
43
{
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
+
60
46
public async Task InvokeAsync ( HttpContext context )
61
47
{
62
48
if ( context == null )
@@ -71,26 +57,26 @@ public async Task InvokeAsync(HttpContext context)
71
57
if ( ShouldBlockUserAgent ( userAgent ) )
72
58
{
73
59
// Log the blocked request if enabled
74
- if ( _options . LogBlockedRequests )
60
+ if ( Options . LogBlockedRequests )
75
61
{
76
- _logger . LogInformation (
62
+ logger . LogInformation (
77
63
"Request blocked from user agent: {UserAgent}, IP: {IPAddress}, Path: {Path}" ,
78
64
userAgent ,
79
65
context . Connection . RemoteIpAddress ,
80
66
context . Request . Path ) ;
81
67
}
82
68
83
69
// Set the response status code
84
- context . Response . StatusCode = _options . BlockedStatusCode ;
70
+ context . Response . StatusCode = Options . BlockedStatusCode ;
85
71
context . Response . ContentType = "text/plain" ;
86
72
87
73
// Write the blocked message to the response
88
- await context . Response . WriteAsync ( _options . BlockedMessage ) ;
74
+ await context . Response . WriteAsync ( Options . BlockedMessage ) ;
89
75
return ;
90
76
}
91
77
92
78
// If not blocked, continue to the next middleware
93
- await _next ( context ) ;
79
+ await next ( context ) ;
94
80
}
95
81
96
82
private bool ShouldBlockUserAgent ( string userAgent )
@@ -102,9 +88,9 @@ private bool ShouldBlockUserAgent(string userAgent)
102
88
return false ;
103
89
}
104
90
105
- foreach ( var blockedAgent in _options . BlockedUserAgents )
91
+ foreach ( var blockedAgent in Options . BlockedUserAgents )
106
92
{
107
- var comparison = _options . IgnoreCase
93
+ var comparison = Options . IgnoreCase
108
94
? StringComparison . OrdinalIgnoreCase
109
95
: StringComparison . Ordinal ;
110
96
0 commit comments