Hi
I have below code but it is returning :: 1
private string GetIpAddress()
{
string IpAddress = string.Empty;
try
{
IpAddress = Request.ServerVariables["HTP_X_FORWARDED_FOR"];
if (IpAddress == " " || IpAddress == null)
{
IpAddress = Request.ServerVariables["REMOTE_ADDR"];
return IpAddress;
}
else
{
return IpAddress;
}
}
catch (ArgumentNullException Exc)
{
Label2.Text = "Application Error : " + Exc.Message;
return IpAddress;
}
catch (InvalidCastException Exc)
{
Label2.Text = "Application Error : " + Exc.Message;
return IpAddress;
}
catch (InvalidOperationException Exc)
{
Label2.Text = "Application Error : " + Exc.Message;
return IpAddress;
}
catch (NullReferenceException Exc)
{
Label2.Text = "Application Error : " + Exc.Message;
return IpAddress;
}
catch (Exception Exc)
{
Label2.Text = "Application Error : " + Exc.Message;
return IpAddress;
}
}
Thanks
Jithu ThomasPosted Jan 21, 2024, 7:22 AM
This code is working in console applicaition. please check.
using System;
using System.Net;
using System.Net.Sockets;
public class IPv4String
{
public static void Main()
{
string ipv4String = GetIPv4Address();
Console.WriteLine("IPv4 Address: " + ipv4String);
}
public static string GetIPv4Address()
{
string ipv4 = string.Empty;
foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipv4 = ip.ToString();
break;
}
}
return ipv4;
}
}
Ramco RamcoPosted Jan 21, 2024, 7:01 AM
Hi
It is showing :: 1 means IP 6 address is taken. I want in IP IV
Thanks
Jithu ThomasPosted Jan 21, 2024, 6:27 AM
https://www.c-sharpcorner.com/UploadFile/167ad2/get-ip-address-using-C-Sharp-code/
https://www.c-sharpcorner.com/article/get-ip-address-in-Asp-Net/
https://www.c-sharpcorner.com/forums/how-to-find-the-client-public-internet-ip-address-in-c-sharp
Please refer this links for better understanding and please accept the answer if you found the same is useful!