I need to get IP address of client in asp.net Controller.
I use this Code:
string ip = string.Empty;
if (request.IsSecureConnection)
ip = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrWhiteSpace(ip))
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrWhiteSpace(ip))
{
ip = request.UserHostAddress;
}
else
{
if (ip.IndexOf(",", StringComparison.Ordinal) > 0)
{
ip = ip.Split(',').Last();
}
}
}
Sometime the ip
is empty.
Do you have any idea why? Why is ip
null in Httpcontext.Current.Request
?