Skip to content

Commit 3bc4670

Browse files
committed
😎修正Nginx反向代理获取真实IP
1 parent 1f934b5 commit 3bc4670

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

backend/Admin.NET.Core/Service/Auth/AuthService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public async Task<LoginOutput> GetLoginUserAsync()
119119
var loginOutput = user.Adapt<LoginOutput>();
120120

121121
loginOutput.LastLoginTime = user.LastLoginTime = DateTimeOffset.Now;
122-
var ip = httpContext.Request.Headers["X-Real-IP"].FirstOrDefault();
123-
loginOutput.LastLoginIp = user.LastLoginIp = string.IsNullOrEmpty(user.LastLoginIp) ? httpContext.GetRemoteIpAddressToIPv4() : ip;
122+
loginOutput.LastLoginIp = user.LastLoginIp = httpContext.GetRemoteIpAddressToIPv4();
124123

125124
//var ipInfo = IpTool.Search(loginOutput.LastLoginIp);
126125
//loginOutput.LastLoginAddress = ipInfo.Country + ipInfo.Province + ipInfo.City + "[" + ipInfo.NetworkOperator + "][" + ipInfo.Latitude + ipInfo.Longitude + "]";

backend/Admin.NET.EntityFramework.Core/Admin.NET.EntityFramework.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
19+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.6" />
2020
<PackageReference Include="Zack.EFCore.Batch.Sqlite" Version="1.2.6" />
2121
</ItemGroup>
2222

backend/Admin.NET.Web.Core/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Furion;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpOverrides;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.Hosting;
910
using OnceMi.AspNetCore.OSS;
@@ -83,6 +84,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8384
app.UseHsts();
8485
}
8586

87+
// NGINX 反向代理获取真实IP
88+
app.UseForwardedHeaders(new ForwardedHeadersOptions
89+
{
90+
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
91+
});
92+
8693
// 添加状态码拦截中间件
8794
app.UseUnifyResultStatusCodes();
8895

backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.5">
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
1111
<PrivateAssets>all</PrivateAssets>
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
</PackageReference>
@@ -18,6 +18,9 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<None Update="Admin.NET.db">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
2124
<None Update="Admin.NET_SaaS.db">
2225
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2326
</None>

0 commit comments

Comments
 (0)