mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:53:25 +08:00
32 lines
862 B
C#
32 lines
862 B
C#
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);
|
|
}
|
|
}
|
|
|
|
}
|