ASP.NET : how to get ip address using csharp(c#) | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

ASP.NET : how to get ip address using csharp(c#)

Thursday

How to get a client's ip address with ASP.NET :
                                         Internet protocol is a communication between the nodes. Internet protocol, which is used to track a system host and used address the location. If we need to identify the user who are all accessing our website and store the Internet protocol address in our database.

                                Fetch a client's Internet protocol address as soon as he access our web site in asp.net. Some of them may use a proxy Internet protocol address. But we can get their Internet protocol address with this simple code.


Code:
//Add the Namespace
using System.Net;
//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 "Your ip is "+stringIpAddress;
}
//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 Visitor Ip Address
string strVisitorIpAddress = GetVisitorIpAddress();

//Get The Lan Ip Address
string strLanIpAddress = GetLanIPAddress();
Share this to your friends. Thanks!

8 comments:

Anonymous said...

good one.. working ....
thnaks yaarr

ipHostEntries.AddressList; Gives Error like:Cannot Implicitly convert type "System.Net.IPAddress[]" to "IPAddress"

Anonymous said...

thank you , but it keep telling me that "Request.ServerVariables" is not in the current context ?!

NeoPulsagram said...

in your get Ip lan function has output for IP provider not lan IP

Anonymous said...

giving ::1 for IPV6

Anonymous said...

It’s never too late to improve your information and your contents inspire me.
iphone backup extractor

Anonymous said...

Super!!!!!

Unknown said...

Awesome .. very helpful!
iPhone photo recovery

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets