ASP.NET CORE获取客户端IP扩展类

public static class HttpContextExtension
    {
        public static string GetClientUserIp(this HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }
    }

你可能感兴趣的:(C#,ASP.NETCORE)