From 3bc467032f3a775b2bb3b1d50f5747cd8c3d307c Mon Sep 17 00:00:00 2001 From: zuohuaijun Date: Wed, 12 May 2021 00:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=E4=BF=AE=E6=AD=A3Nginx=E5=8F=8D?= =?UTF-8?q?=E5=90=91=E4=BB=A3=E7=90=86=E8=8E=B7=E5=8F=96=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Admin.NET.Core/Service/Auth/AuthService.cs | 3 +-- .../Admin.NET.EntityFramework.Core.csproj | 2 +- backend/Admin.NET.Web.Core/Startup.cs | 7 +++++++ backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj | 5 ++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/Admin.NET.Core/Service/Auth/AuthService.cs b/backend/Admin.NET.Core/Service/Auth/AuthService.cs index 79c472f05..6c045ab93 100644 --- a/backend/Admin.NET.Core/Service/Auth/AuthService.cs +++ b/backend/Admin.NET.Core/Service/Auth/AuthService.cs @@ -119,8 +119,7 @@ public async Task GetLoginUserAsync() var loginOutput = user.Adapt(); loginOutput.LastLoginTime = user.LastLoginTime = DateTimeOffset.Now; - var ip = httpContext.Request.Headers["X-Real-IP"].FirstOrDefault(); - loginOutput.LastLoginIp = user.LastLoginIp = string.IsNullOrEmpty(user.LastLoginIp) ? httpContext.GetRemoteIpAddressToIPv4() : ip; + loginOutput.LastLoginIp = user.LastLoginIp = httpContext.GetRemoteIpAddressToIPv4(); //var ipInfo = IpTool.Search(loginOutput.LastLoginIp); //loginOutput.LastLoginAddress = ipInfo.Country + ipInfo.Province + ipInfo.City + "[" + ipInfo.NetworkOperator + "][" + ipInfo.Latitude + ipInfo.Longitude + "]"; diff --git a/backend/Admin.NET.EntityFramework.Core/Admin.NET.EntityFramework.Core.csproj b/backend/Admin.NET.EntityFramework.Core/Admin.NET.EntityFramework.Core.csproj index 158909413..f96d49db2 100644 --- a/backend/Admin.NET.EntityFramework.Core/Admin.NET.EntityFramework.Core.csproj +++ b/backend/Admin.NET.EntityFramework.Core/Admin.NET.EntityFramework.Core.csproj @@ -16,7 +16,7 @@ - + diff --git a/backend/Admin.NET.Web.Core/Startup.cs b/backend/Admin.NET.Web.Core/Startup.cs index 0b5cae14e..4e9246c32 100644 --- a/backend/Admin.NET.Web.Core/Startup.cs +++ b/backend/Admin.NET.Web.Core/Startup.cs @@ -4,6 +4,7 @@ using Furion; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using OnceMi.AspNetCore.OSS; @@ -83,6 +84,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseHsts(); } + // NGINX 反向代理获取真实IP + app.UseForwardedHeaders(new ForwardedHeadersOptions + { + ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto + }); + // 添加状态码拦截中间件 app.UseUnifyResultStatusCodes(); diff --git a/backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj b/backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj index 3aeef0af7..d768816be 100644 --- a/backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj +++ b/backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj @@ -7,7 +7,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -18,6 +18,9 @@ + + PreserveNewest + PreserveNewest