0

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!

kvacka
  • 25
  • 6
  • the answer is simple: ***you can't***. and you shouldn't be able to, either. nor should you use an IP-address, local or not - which can change at any moment - to identify a device. whatever you're trying to achieve, best think of another way to do it. or if it is even necessary. – Franz Gleichmann Aug 18 '20 at 16:31
  • if your gateway using any firewall then you must configure X-headers such as "x-forwarded-for", here is the fortiweb docs: https://kb.fortinet.com/kb/documentLink.do?externalID=FD36172 – pka246 Aug 18 '20 at 17:42

3 Answers3

0

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"];
pka246
  • 193
  • 10
0

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()  
        : "";  
}
  • It 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. – kvacka Aug 20 '20 at 09:13
  • A solution has been provided for this question [here](https://stackoverflow.com/questions/28664686/how-do-i-get-client-ip-address-in-asp-net-core/41335701#41335701) it will help you – Dilip Singh Aug 20 '20 at 11:56
-1

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