Willcode4cash icon

Capture IP Address

Willcode4cash | PRO | 09/13/16 02:11:04 PM UTC | 0 ⭐ | 262 👁️ | Never ⏰ | []
C# |

700 B

|

None

|

0 👍

/

0 👎

public static string GetIpAddress(this HttpContext ctx)
{
    try
    {
        if (!string.IsNullOrEmpty(ctx.Request.ServerVariables["HTTP_CLIENT_IP"]))
        {
            return ctx.Request.ServerVariables["HTTP_CLIENT_IP"];
        }
        if (!string.IsNullOrEmpty(ctx.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
        {
            var addresses = ctx.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new[] { ',' },
                StringSplitOptions.RemoveEmptyEntries).ToList();
            return addresses.First();
        }
        return ctx.Request.ServerVariables["REMOTE_ADDR"];
    }
    catch (Exception)
    {
        // Log exception
        throw;
    }
}

Comments