Skip to content

Instantly share code, notes, and snippets.

@pparadis

pparadis/ip.cs

Last active Dec 18, 2015
Embed
What would you like to do?
Un gist basé sur le code de http://stackoverflow.com/a/2620956/1291
public static IPAddress GetIp(this System.Web.HttpRequest request)
{
string ipString;
if (string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ipString = request.ServerVariables["REMOTE_ADDR"];
}
else
{
ipString = request
.ServerVariables["HTTP_X_FORWARDED_FOR"]
.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
}
IPAddress result;
if (!IPAddress.TryParse(ipString, out result))
{
result = IPAddress.None;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment