| C# Sources |
| 1 | using System; using System.Linq; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; |
| 2 | namespace ClientIpAspNetCore { #region snippet_ClassOnly public class AdminSafeListMiddleware { private readonly RequestDelegate _next; private readonly ILogger<AdminSafeListMiddleware> _logger; private readonly string _adminSafeList; public AdminSafeListMiddleware( RequestDelegate next, ILogger<AdminSafeListMiddleware> logger, string adminSafeList) { _adminSafeList = adminSafeList; _next = next; _logger = logger; } |
| 3 | public async Task Invoke(HttpContext context) { if (context.Request.Method != "GET") { var remoteIp = context.Connection.RemoteIpAddress; _logger.LogDebug($"Request from Remote IP address: {remoteIp}"); string[] ip = _adminSafeList.Split(';'); var bytes = remoteIp.GetAddressBytes(); var badIp = true; foreach (var address in ip) { var testIp = IPAddress.Parse(address); if(testIp.GetAddressBytes().SequenceEqual(bytes)) { badIp = false; break; } } if(badIp) { _logger.LogInformation( $"Forbidden Request from Remote IP address: {remoteIp}"); context.Response.StatusCode = 401; return; } } await _next.Invoke(context); } } #endregion } |
Комментарии