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; } }