Hi Guys,
I was given a task to get the IP Address of a computer. Most of the times when we are working we have two IP's one assigned on the LAN network and one assigned by the internet ISP. I was given a task to find both of them. Here is how i was able to create the same.
//For ISP (Internet Service Provider) IP Address
public static string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string sIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(sIPAddress))
{
return context.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
string[] ipArray = sIPAddress.Split(new Char[] { ',' });
return ipArray[0];
}
}
//For LAN (Local Area Network) IP Address
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
Thanks
Ritesh Tandon
I was given a task to get the IP Address of a computer. Most of the times when we are working we have two IP's one assigned on the LAN network and one assigned by the internet ISP. I was given a task to find both of them. Here is how i was able to create the same.
//For ISP (Internet Service Provider) IP Address
public static string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string sIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(sIPAddress))
{
return context.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
string[] ipArray = sIPAddress.Split(new Char[] { ',' });
return ipArray[0];
}
}
//For LAN (Local Area Network) IP Address
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
Thanks
Ritesh Tandon
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b