@manhng

Welcome to my blog!

Get the IP Address in .NET

June 18, 2018 20:56

Get the IP Address in .NET (edit)

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

Get IP Address In ASP.NET

https://www.c-sharpcorner.com/article/get-ip-address-in-Asp-Net/

Get the IP Address in .NET Core

        private string GetIpAddress()
        {
            // get source ip address for the current request
            if (Request.Headers.ContainsKey("X-Forwarded-For"))
                return Request.Headers["X-Forwarded-For"];
            else
                return HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
        }

Categories

Recent posts