Skip to content

Commit

Permalink
😎修正Nginx反向代理获取真实IP
Browse files Browse the repository at this point in the history
  • Loading branch information
zuohuaijun committed May 11, 2021
1 parent 1f934b5 commit 3bc4670
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 1 addition & 2 deletions backend/Admin.NET.Core/Service/Auth/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public async Task<LoginOutput> GetLoginUserAsync()
var loginOutput = user.Adapt<LoginOutput>();

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 + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.6" />
<PackageReference Include="Zack.EFCore.Batch.Sqlite" Version="1.2.6" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions backend/Admin.NET.Web.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion backend/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.5">
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -18,6 +18,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Admin.NET.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Admin.NET_SaaS.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 3bc4670

Please sign in to comment.