Skip to content

Commit 1ac6417

Browse files
committed
Update UserAgentBlocking
1 parent 90e1933 commit 1ac6417

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

MyApp/Configure.UserAgentBlocking.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Threading.Tasks;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.Extensions.Logging;
61
using Microsoft.Extensions.Options;
72

83
namespace 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

MyApp/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
services.AddBlazorServerIdentityApiClient(baseUrl);
6363
services.AddLocalStorage();
6464

65-
// Register the options in the dependency injection container
6665
services.Configure<UserAgentBlockingOptions>(options =>
6766
{
6867
// Add user agents to block

0 commit comments

Comments
 (0)