You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1015 B
C#

3 years ago
using Microsoft.AspNetCore.Http;
using System;
namespace Common
{
/// <summary>
/// 自定义httpContext
/// </summary>
public class MyHttpContext
{
private static IHttpContextAccessor _accessor;
public static HttpContext Current => _accessor.HttpContext;
static MyHttpContext() { }
public static void Configure(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
/// <summary>
/// 获取客户端请求Id
/// </summary>
/// <returns></returns>
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;
}
}
}