-1

I am trying to get the current users IP address and it works locally but now that my project is live it isn't grabbing an IP address at all.

Here is what I am doing that worked locally:

var context = new PrincipalContext(ContextType.Domain);
        var principal = UserPrincipal.FindByIdentity(context, System.Web.HttpContext.Current.User.Identity.Name);
  • You are probably getting the machine name instead of IP. Do you need IP or is Host-name acceptable? – jdweng Jul 28 '20 at 15:56
  • @jdweng yes, very sorry. I just updated the code. I pasted the wrong lines.I need the IP address. – ProgrammingIsLife Jul 28 '20 at 15:57
  • What are you **actually** trying to accomplish? Why does your web app need the user's IP address? – Daniel Mann Jul 28 '20 at 15:59
  • @DanielMann it is an internal application and i need to store the users IP address in a database when they use this specific app. – ProgrammingIsLife Jul 28 '20 at 16:00
  • "i need to store the users IP address in a database" ...that's still the requirement, not the reason. Why do you want to store the IP address? What purpose will that have? Also, a single user logging into an application could easily do so from multiple different IP addresses on different occasions. Equally, due to technology such as NAT, multiple users can all appear to have the same (public) IP address. So the concept of "the user's IP address" is somewhat flawed to begin with - there's rarely any such thing on the web these days – ADyson Jul 28 '20 at 18:58

1 Answers1

0

Try this

var ipAdress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];