read client IP address

I have a website and try to track client IP to locate his/her for better information tracking.
we use c#/mvc/razor.

any suggestions on what we can use?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
In the controller you should be able to use:


kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Do note that if your infrastructure has any kind of (reverse) proxy server or similar appliance fronting your web server(s), then you may need to configure that appliance to include the X-Forwarded-For header in order to get that information sent to your web server.
ITsolutionWizardAuthor Commented:
kaufmed   ¯\_(ツ)_/¯: can you describe little more? I just want to get IP from the client side.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ITsolutionWizardAuthor Commented:
Bill Prew: Your way does not work. It always return to my server location. I try my office, at home, and it still return to our server location.
at home i use and put my home ip address, it confirms the city is at my city.
Shaun VermaakTechnical SpecialistCommented:
If you just want it from a logging point of view, and not within the actual application, use IIS logs
Bill PrewIT / Software Engineering ConsultantCommented:
Haven't tested this here, but here's an interesting code snip that you could test out...

Get Client IP Address in ASP.NET

/// <summary>
/// Attempts to retrieve the real client ip (w/ proxy detection).
/// </summary>
/// <returns>The IP address of the remote client,
/// or "" if it cannot be obtained.</returns>
public static string GetClientIpAddress()
    const string defaultValue = "";
    HttpContext context;
    if ((context = HttpContext.Current) == null)
        return defaultValue;
    HttpRequest request;
        // store a local ref to the request object:
        request = context.Request;
    catch (HttpException)
        Debug.WriteLine("Http request context is not available.");
        return defaultValue;
    // check proxy forwarder indicators first:
    string forwarderForIp = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(forwarderForIp))
        int firstCommaIndex = forwarderForIp.IndexOf(',');
        if (firstCommaIndex > 0)
            // X-Forwarded-For can return multiple ip's, so 
            // just get the first one.
            // ex: X-Forwarded-For: client, proxy1, proxy2
            forwarderForIp = forwarderForIp.Substring(0, firstCommaIndex).Trim();
        if (!forwarderForIp.Equals("unknown",
            return forwarderForIp;
    // get the client ip from server from server vars:
    string remoteAdd = request.ServerVariables["REMOTE_ADDR"];
    return !string.IsNullOrEmpty(remoteAdd) ? remoteAdd : defaultValue;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.