Wednesday, October 2, 2013

Get My (Current) IP and Hosted file IP

To Retrieve My machine IP Address using in c# you just need to write a single line i.e.

Request.UserHostAddress

And you  will receive your IP


================================================================

To Get Hosted file IP Address you need to add this much code on your site

using System.Net;
using System.Net.NetworkInformation;

protected void Page_Load(object sender, EventArgs e)
    {
Response.Write("My Ip Address : 1) " + GetIpAddress() + "      2) " + Request.UserHostAddress + "<br/>");
        Response.Write("My Hosted Address Ip : 1) " + GetHostedIP());
    }

 public string GetMyIpAddress()
    {
        string stringIpAddress;
        stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (stringIpAddress == null)
        {
            stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];
        }
        return stringIpAddress;
    }

private string GetHostedIP()
    {
        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();
        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
        IPAddress[] addr = ipEntry.AddressList;
        return addr[addr.Length - 1].ToString();
    }

No comments:

Post a Comment