How to get the Machine Public Ip address of the Client computer ?

ViShal pawar 0 Reputation points
2024-01-25T12:26:26.1266667+00:00
Hi Team,

I need to get the local machine ip address of the client which uses our website.
Our Web application is build on .Net Core 3.1 , and hosted in Azure app service.
Basically we have a custom developer portal same as the apim developer portal where the client/user can test the http(GET/POST/PUT/DELETE) apis.
We are whitelisting the Product(contains apis) using the ip-filter policy so that only dedicated client/user can access those apis.

To do that we are unable to get the Ips using C# code.

Below are the ways we have tried -  
1)
  IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
                List
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-01-25T13:13:14.1066667+00:00

    Please recommend some solution to get the current local machine ip of the Client.

    You might need to check the HTTP_X_FORWARDED_FOR variable too if the client is behind a proxy or you have a network device between the client and your web application.

    context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    

    Keep in mind, using HTTP_X_FORWARDED_FOR does not yield the local machine IP of the Client. This information is simply not available over TCP. That is, if I understand what you're trying to do. The web server and client need to be on the same network in order for the web server to see the client's local IP. The best you can get is Client's WAN address which might change over time depending on how the client's network works.
    Perhaps create an authentication/authorization solution rather than IP.

    1 person found this answer helpful.
    0 comments No comments

  2. Anonymous
    2024-01-26T06:21:16.0866667+00:00

    Hi,@ViShal pawar, you could refer to AgaveJoe's answer,

    Also,here's a build-in api to get the ipaddress: HttpContext.Connection.RemoteIpAddress


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards, Ruikai Feng

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.