diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs new file mode 100644 index 00000000..d00fd95c --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs @@ -0,0 +1,37 @@ +using System.Net; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Volo.Abp.AspNetCore.WebClientInfo; + +namespace Yi.Framework.AspNetCore; + +public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoProvider +{ + public RealIpHttpContextWebClientInfoProvider(ILogger logger, + IHttpContextAccessor httpContextAccessor) : base(logger, httpContextAccessor) + { + } + + protected override string? GetClientIpAddress() + { + try + { + var httpContext = HttpContextAccessor.HttpContext; + + var headers = httpContext?.Request?.Headers; + + if (headers != null && headers.ContainsKey("X-Forwarded-For")) + { + httpContext.Connection.RemoteIpAddress = + IPAddress.Parse(headers["X-Forwarded-For"].FirstOrDefault()); + } + + return httpContext?.Connection?.RemoteIpAddress?.ToString(); + } + catch (Exception ex) + { + Logger.LogException(ex, LogLevel.Warning); + return null; + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs index ad551e70..006a3763 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs @@ -11,6 +11,7 @@ using Newtonsoft.Json.Linq; using Swashbuckle.AspNetCore.SwaggerGen; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; using Yi.Framework.AspNetCore.Mvc; @@ -22,6 +23,11 @@ namespace Yi.Framework.AspNetCore )] public class YiFrameworkAspNetCoreModule : AbpModule { - + public override void PostConfigureServices(ServiceConfigurationContext context) + { + var services = context.Services; + services.Replace(new ServiceDescriptor(typeof(IWebClientInfoProvider), + typeof(RealIpHttpContextWebClientInfoProvider), ServiceLifetime.Transient)); + } } } \ No newline at end of file