Header Ads

Get IP Address Using Asp.Net and C#

Here I am giving two methods to find the Internet Protocol Address

Method - 1 : Get Visitor IP address method

using System.Net;//Add the Namespace
//Get Visitor IP address method
public string GetVisitorIpAddress()
{
string stringIpAddress;
stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
{
stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
}
return "Visitor IP is "+stringIpAddress;
}

//Get The Visitor Ip Address
string strVisitorIpAddress = GetVisitorIpAddress();

Method - 2 : Get Lan Connected IP address method


public string GetLanIPAddress()
{
//Get the Host Name
string stringHostName = Dns.GetHostName();
//Get The Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;
return arrIpAddress[arrIpAddress.Length - 1].ToString();
}

//Get The Lan Ip Address
string strLanIpAddress = GetLanIPAddress();

No comments:

Powered by Blogger.