Get client IP Address in MVC 3.0


You can directly use the below function to your controller page.

Public string GetIp()
{
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ip))
        {
            ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        return ip;
}
The above function will return client ip address.It is beeter to use than Request.UserHostAddress() 
because sometimes it only capture the IP address of a user's proxy
Ebook Download
View all
Learn
View all