mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:13:26 +08:00
【新增】增加获取用户真实ip地址中间件【RealIpMiddleware】,防止Nginx转发后只能获取服务器ip的问题。
This commit is contained in:
@@ -30,6 +30,16 @@ namespace CoreCms.Net.Middlewares
|
||||
return app.UseMiddleware<RequRespLogMildd>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户访问真实ip
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder RealIpMiddleware(this IApplicationBuilder app)
|
||||
{
|
||||
return app.UseMiddleware<RealIpMiddleware>();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异常处理中间件(后端模式)
|
||||
@@ -67,7 +77,7 @@ namespace CoreCms.Net.Middlewares
|
||||
/// <param name="app"></param>
|
||||
/// <param name="dataSources"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app,string dataSources)
|
||||
public static IApplicationBuilder UseRecordAccessLogsMildd(this IApplicationBuilder app, string dataSources)
|
||||
{
|
||||
return app.UseMiddleware<RecordAccessLogsMildd>(dataSources);
|
||||
}
|
||||
|
||||
31
CoreCms.Net.Middlewares/RealIpMiddleware.cs
Normal file
31
CoreCms.Net.Middlewares/RealIpMiddleware.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreCms.Net.Middlewares
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取真实ip地址
|
||||
/// </summary>
|
||||
public class RealIpMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next; public RealIpMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
public Task Invoke(HttpContext context)
|
||||
{
|
||||
var headers = context.Request.Headers;
|
||||
if (headers.ContainsKey("X-Forwarded-For"))
|
||||
{
|
||||
context.Connection.RemoteIpAddress = IPAddress.Parse(headers["X-Forwarded-For"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)[0]);
|
||||
}
|
||||
return _next(context);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -158,6 +158,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
#endregion
|
||||
|
||||
#region 中间件注册===================================================================
|
||||
// 获取用户访问真实ip
|
||||
app.RealIpMiddleware();
|
||||
// 启用并发限制数中间件
|
||||
app.UseConcurrencyLimiter();
|
||||
// 开启Ip限流
|
||||
@@ -168,6 +170,7 @@ app.UseRequestResponseLog();
|
||||
app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Admin.ToString());
|
||||
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
||||
app.UseIpLogMildd();
|
||||
|
||||
#endregion
|
||||
|
||||
app.UseSwagger().UseSwaggerUI(c =>
|
||||
|
||||
@@ -171,6 +171,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
#endregion
|
||||
|
||||
#region 中间件注册===================================================================
|
||||
// 获取用户访问真实ip
|
||||
app.RealIpMiddleware();
|
||||
// 启用并发限制数中间件
|
||||
app.UseConcurrencyLimiter();
|
||||
// 开启Ip限流
|
||||
|
||||
Reference in New Issue
Block a user