简体   繁体   中英

How to get local device IP in ASP.NET Core MVC

I'm working on a ASP.NET Core project where I have a Login form, and I want to identify device from where the user is trying to login. This is what I find and what I use to get the IP:

userLogin.stringIP=Request.HttpContext.Connection.RemoteIpAddress.ToString();

With this I get only Local Gateway IP instead of local IP of device.

Update

The example code works ,but when I publish the Website on my IIS Server it gives me the IP of IIS server, instead of device from where I try to acces the Website For example when I open the Website that is published on IIS server it gaves me the IP of IIS server.I need the device IP address from which I try to access the WebSite.

Any ideas / suggestions how to get local IP of device.

Thanks in advance!

Try any one from the below:

 var ip1 = Request.HttpContext.Connection.LocalIpAddress.ToString();
 var ip2 = Request.HttpContext.Connection.RemoteIpAddress.ToString();
 var ip3 = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress.ToString();
 var ip4 = HttpContext.Features.Get<IHttpConnectionFeature>()?.LocalIpAddress.ToString();

 string remoteIpAddress = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
 if (Request.Headers.ContainsKey("X-Forwarded-For"))
     remoteIpAddress = Request.Headers["X-Forwarded-For"];

The below code will return the local IP Address:

publicl static string GetLocalIpAddress()  
{  
    UnicastIPAddressInformation mostSuitableIp = null;  
    var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();  
    foreach (var network in networkInterfaces)  
    {  
        if (network.OperationalStatus != OperationalStatus.Up)  
            continue;  
  
        var properties = network.GetIPProperties();  
        if (properties.GatewayAddresses.Count == 0)  
            continue;  
  
        foreach (var address in properties.UnicastAddresses)  
        {  
            if (address.Address.AddressFamily != AddressFamily.InterNetwork)  
                continue;  
  
            if (IPAddress.IsLoopback(address.Address))  
                continue;  
  
            if (!address.IsDnsEligible)  
            {  
                if (mostSuitableIp == null)  
                    mostSuitableIp = address;  
                continue;  
            }  
  
            // The best IP is the IP got from DHCP server  
            if (address.PrefixOrigin != PrefixOrigin.Dhcp)  
            {  
                if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)  
                    mostSuitableIp = address;  
                continue;  
            }  
            return address.Address.ToString();  
        }  
    }  
    return mostSuitableIp != null  
        ? mostSuitableIp.Address.ToString()  
        : "";  
}

If the asp.net core project is located outside the firewall of the local device from which the user is logging in and the user is using a private IP address (RFC 1918) then his/her local firewall will strip out the private IP address and replace that with the public facing IP address as source IP address.

Have you tried the solution provided by mrchief in Get local IP address ?

He (she?) uses Dns.GetHostEntry(Dns.GetHostName()) to get the local IP address of the machine.

Worth trying.

Regards N

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM