using Microsoft.AspNetCore.Http; using System; namespace Common.Tools { /// /// 自定义httpContext /// public class MyHttpContext { private static IHttpContextAccessor _accessor; public static HttpContext Current => _accessor.HttpContext; static MyHttpContext() { } public static void Configure(IHttpContextAccessor accessor) { _accessor = accessor; } /// /// 获取客户端请求Id /// /// public static string GetClientIP() { string result = "127.0.0.1"; try { result = Current.Request.Headers["X-Forwarded-For"]; if (null == result || result == String.Empty) { result = Current.Request.Headers["X-Real-IP"]; } } catch { } return result; } } }