I have a problem with getting visitor ip address I am using asp net mvc I used the following code to save the visitors IP addresses bur unfortunately It didnt work on my local network many visited my page from the LAN but didnt catch any of them , I had a website of many pages and if users visited the another page also a problem it wont catch at all because I have the code in the Home index please let me know what is wrong with my code and where I have to place the code in the project so it will catch and visitors for any page if the user clicked a link rather the home here is my code home controller
public ActionResult Index()
{
string currip = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
currip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0)
{
currip = System.Web.HttpContext.Current.Request.UserHostAddress;
}
// string currip = HttpContext.Request.UserHostAddress.ToString(); // tried this didnt work it gave me single ip for all visitors
// vs.Ip = currip;
vs.Ip = HttpContext.Request.UserHostAddress.ToString();
vs.lastaccess = DateTime.Now;
vs.visits += 1;
_db.SaveChanges();
return view();
}
UPDATE when using currip = System.Web.HttpContext.Current.Request.UserHostAddress; for post method adding login information ip appears but for visit it doesnt appear because it is get method anything to do with that ?