a blog for those who code

Thursday, 26 June 2014

C# : Get Visitor IP Address

In this post we will show you how to get IP Address of the Visitor in C#. The below code snippet will help you to get the Visitor IP address...


Code : 

string GetVisitorIpAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if(!string.IsNullOrEmpty(ipAddress))
{
string[] ipAddresses = ipAddress.Split(',');
if(ipAddresses.Length != 0)
{
return ipAddresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment