Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get client ip address in static class in asp.net mvc

I want to get ip address of client in static class in asp.net mvc 3.

But i can't access request object in static class.

can any one help how to get ip address without request object in static class??

like image 233
iChirag Avatar asked Jan 27 '12 10:01

iChirag


1 Answers

You can get the user's IP address in a static class like this:

        string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ip))
        {
            ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        return ip;

This technique is better to use that Request.UserHostAddress() as that will sometimes only capture the IP address of a user's proxy.

like image 83
Mark Avatar answered Sep 20 '22 18:09

Mark