Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save the IP of the customers who registers with my application and save theirs ips into database.In my case my dev server ip is saving into db

What I have tried:

var ip = HttpContext.Connection.RemoteIpAddress

its saves the ip of my dev server.
Posted
Updated 9-Nov-17 1:59am

Short Answer:
C#
private string GetUserIP()
    {
        string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipList))
        {
            return ipList.Split(',')[0];
        }

        return Request.ServerVariables["REMOTE_ADDR"];
    }

reference: c# - How to get a user's client IP address in ASP.NET? - Stack Overflow[^]

Long Answer: Read the below discussion, specially those with huge up votes.
c# - How to get a user's client IP address in ASP.NET? - Stack Overflow[^]

Hope, it helps :)
 
Share this answer
 
Check where your dev server is: normally in development it's on your machine, so it will indeed have the same IP address as the client!

Be aware that in web applications, the IP address will not be individual to a client computer - it will be the IP of the router via which he connects, so an entire office will share the same IP address as far as your server is concerned.
And be aware that most IP addresses aren't static - they can change each time the device is connected, so using them as a security mechanism isn't going to work reliably, if that was your intent.
 
Share this answer
 
String IP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (string.IsNullOrEmpty(IP))
IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900