0

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 ?

NAZ HUMASH
  • 27
  • 9
  • That might be due to firewall/DMZ/NAT. – Peter Smith Aug 16 '20 at 14:59
  • Does this answer your question? [How can I get the client's IP address in ASP.NET MVC?](https://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc) – Progman Aug 16 '20 at 15:02
  • I have no idea about that , we are on the testing stage so I am not sure if we host it whether it will work or no with current code, I had tried it if user sign in then it worked fine with this currip = System.Web.HttpContext.Current.Request.UserHostAddress; but not visiting – NAZ HUMASH Aug 16 '20 at 15:05
  • @Progman no it didnt – NAZ HUMASH Aug 16 '20 at 15:15
  • Bear in mind that you might need to tweak your firewall/forwarder/load balancer configuration to pass on any of these headers – Neil Aug 16 '20 at 15:34

2 Answers2

1
public static string getIPAddress(HttpRequestBase request)
    {
        string ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = request.ServerVariables["REMOTE_ADDR"];
        }

        return ipAddress;
    }

UPDATE

You can use Visit Attribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class VisitAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var descriptor = filterContext.ActionDescriptor;
        var controller = descriptor.ControllerDescriptor.ControllerName;
        var action = descriptor.ActionName;
        var user = filterContext.HttpContext.User.Identity.Name;

        // your code here 
     }
}

Then you can call form your controller or action like this:

[Visit]
public class YourController : Controller
{

}
Wowo Ot
  • 1,362
  • 13
  • 21
  • Thank you @Wowo Ot , if i want to call this function when the application starts from any page rather the home page where to place it and what argument I should pass in the calling function? – NAZ HUMASH Aug 16 '20 at 15:09
  • You could add this as middleware, to extract it once, and then pass it through to each page as a session variable. – Neil Aug 16 '20 at 15:34
  • your answer looks logically correct but I dont know how to implement it could you please let me know how make all this together , your code here you mean i should place the first part of the answer in the there please clarify – NAZ HUMASH Aug 16 '20 at 16:22
  • Yes , you will place "getIPAddress" and your code for inserting in the database. The "VisitAttribute" class you can put it inside any class or create an new class for it. – Wowo Ot Aug 16 '20 at 16:27
  • thanks alot please let me know the parameter to pass inside function string curip = getIPAddress( here what to write ); – NAZ HUMASH Aug 16 '20 at 16:35
  • filterContext.RequestContext.HttpContext.Request – Wowo Ot Aug 16 '20 at 16:45
0

additional to Wowo Ot answer which i tried but didnt work and the answer was logically correct but the problem with my code was old code

vs.visits += 1;
_db.SaveChanges();   

I had to edit my code to

vs.visits += 1;
_db.visit.add(vs); // here was the missing code 
_db.SaveChanges();   
NAZ HUMASH
  • 27
  • 9