HTTPContext.Current.Request Ip address is empty - c#

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?

Related

Get Public client IP in C#/.net without external service

How i get public client IP in C# without external api/service
I tried use this code. But, not success.
string ipAddress = Response.HttpContext.Connection.RemoteIpAddress.ToString();
if(ipAddress == "::1")
{
ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
}
Console.WriteLine(ipAddress);
on ASP.NET Follow This Method:
Public string GetIp()  
{  
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
if (string.IsNullOrEmpty(ip))  
{  
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
}  
return ip;  
} 
otherwise use below method:
Public string GetIp()  {
string IPAddress = "";
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList)
{
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPAddress = Convert.ToString(IP);
}
}
return IPAddress;
}
Please up-vote if you found this helpful.

How to Get system IP (IPv4) address and convert to string using C# asp.net

On my web application I am using the following function to get System IP
Function
public void SetHostid()
{
try
{
string ip = "";
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
ip = addr[1].MapToIPv4().ToString();
HostId = ip;
HttpContext.Current.Session["Hostid"] = HostId;
}
catch (Exception ex)
{
Error_ManagerClass em = new Error_ManagerClass();
em.WriteError(ex);
}
}
It works perfectly because the IP is on the 1 postion of the variable addr (addr[ 1]).
And the problem comes when I try to run the same solution from a different system. function throws an error while trynig to convert IP to string( ip = addr[1].MapToIPv4().ToString(); ) because IP is not in the position number 1.
how can I change the function to work on every computer ??
If you want to get IPv4 only use this code:
var addr = ipEntry.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork);
var firstInList = addr.First(); // get first
But you should consider which IP to chose when there are several IP addresses in system.

Get Client external IP or Client LAN IP

public static string GetClientExternalIp()
{
HttpContext context = HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
I am struggling with the above code snippet, i want to get the client external IP address like what you see when browse to http://checkip.dyndns.org but above snippet returns the IP address of the Server. What i need is the IP address of the LAN the client is connecting from NOT the web server IP.
Use HttpRequest.UserHostAddress
HttpContext.Current.Request.UserHostAddress;
I use the above line of code and it returns the clients IP address.
Try this..
This works for me.
By using this method you can get the client IP address instead of Server IP.
public static string GetClientIP()
{
try
{
string VisitorsIPAddress = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
VisitorsIPAddress = HttpContext.Current.Request.UserHostAddress;
}
return VisitorsIPAddress;
}
catch (Exception)
{
return null;
}
}

Get IP address of client machine

I am trying to get the IP address of client machine using C#. I am using the below code to get the IP address :
string IPAddress = HttpContext.Current.Request.UserHostAddress;
But it is giving me the response in encoded format i.e fe80::ed13:dee2:127e:1264%13
How can I get the actual IP address? Any one faced this issue please share some idea.
C#
string IPAddress = GetIPAddress();
public string GetIPAddress()
{
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList) {
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
IPAddress = Convert.ToString(IP);
}
}
return IPAddress;
}
VB.net
Dim Host As IPHostEntry
Dim Hostname As String
Hostname = My.Computer.Name
Host = Dns.GetHostEntry(Hostname)
For Each IP As IPAddress In Host.AddressList
If IP.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
IPAddress = Convert.ToString(IP)
End If
Next
Return IPAddress
Hope this helps
private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
You may get several ip address, so can split them as-
private string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}
return Request.ServerVariables["REMOTE_ADDR"];
}
try using this
string ip=System.Net.Dns.GetHostEntry
(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
In my project it's required to get the local PC IP. So i use it
Please try the below code
string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string ip = addr[1].ToString();

Getting the ip adress of the user connected to asp application

I'd like to know how to get the IP of the user connected to my application ( asp.net mvc4) .
I tried:
IPHostEntry ipHostEntry = Dns.GetHostByName(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];
But it didn't work.
So how can I modify the snippet to get the Ip` or the connected user?
Here's the converted C# code from the similar question #jamieHennerley suggested.
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}

Categories