private string GetClientIpAddress(System.Net.Http.HttpRequestMessage request)
{
// Check if the request contains a header with the user's IP address
if (request.Headers.Contains("X-Forwarded-For"))
{
return request.Headers.GetValues("X-Forwarded-For").FirstOrDefault();
}
// If the header is not available, fallback to the standard method of getting the IP address
else if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((System.Web.HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
// If all else fails, return an empty string or handle the situation as needed
else
{
return string.Empty;
}
}
this.GetClientIpAddress(Request);