Thursday, 4 May 2017

How to get Client IP Address of Visitors Machine in MVC

Get IP Address
Description:
In this example we explain that how to get client IP Address of Visitor machine in asp.net MVC.or how can I get Client IP Address of visitor machine in mvc.or how to get IP Address of the user in asp.net mvc.

Here we explain that how to get IP Address of visitors machine which don’t use proxy servers. Here we use HTTP_X_FORWARDED_FOR to check the machine use any proxy server or not and second we use REMOTE_ADDR to get the client machine IP Address.

Below is the example that demonstrates how to get or fetch client machine IP Address using asp.net MVC.

Controller:

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        string clientIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(clientIPAddress))
        {
            clientIPAddress = Request.ServerVariables["REMOTE_ADDR"];
        }
        ViewBag.ClientIPAddress = clientIPAddress;

        return View();
    }
}

View:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-familyArial;
            font-size10pt;
        }
    </style>
</head>
<body>
    <h1>
        Your IP Address: @ViewBag.ClientIPAddress
    </h1>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment