Why is a REST call from the same machine not resolving to localhost IP?

  Kiến thức lập trình

We have a legacy REST client which lives on both the app-server and other machines, which makes calls to the app-server with an URL like http://MY-APP-SERVER-NAME:1234/Endpoint.

In the ASP.net handler we want to check if the request has come from the local machine and have code like this in our controller:

[HttpPut, Route("/Endpoint")]
public async Task<ActionResult> Method()
    var sourceIp = request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

    if (sourceIp.Equals(""))...

What we’re observing is that userIpAddress is taking some unexpected value like Both RemoteIpAddress and LocalIpAddress are the same IPv6 address configured for the local machine’s virtual network adapter.

I wonder if the client should be making calls to localhost instead of MY-APP-SERVER-NAME but this code is harder to change.

It seems like it should be easy to tell if the request has come from the machine it is received on, is there some simple utility?