2

I have made a website in .net core MVC. My aim is to try and get some functionality hidden behind whether the user is on the local network or not.

I am running my website inside a linux container hosted on docker for windows. Currently, every user that reaches the site has the ip '::ffff:172.17.0.1'. This originally made the website throw an error. I have put some error handling now so this doesnt throw.

Issue with the above is as every user regardless has the ip: '::ffff:172.17.0.1'. All users are appearing as on the local network, when they are not.

I have put this inside my startup.cs file in the configure method

  app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor |
            ForwardedHeaders.XForwardedProto
        });

And this is my method for checking whether the user is on local network or not:

        var remoteIpAddress = context.Connection.RemoteIpAddress;
        var networkDetails = new UserNetworkInfomation();

        if (remoteIpAddress == null)
            return networkDetails;

        var isLocalNetwork = remoteIpAddress.AddressFamily == AddressFamily.InterNetworkV6;

        try
        {
            var userIpAddress = isLocalNetwork ? GetHostPublicIP() : $"{remoteIpAddress}";

            _logger.LogInformation($"User({remoteIpAddress}) on local network: {isLocalNetwork}");

            networkDetails.IpAddress = userIpAddress;
            networkDetails.IsLocalNetwork = isLocalNetwork;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, ex.Message);
        }

        return networkDetails;

The context that is passed into the method above is from the HomeController:

public IActionResult Index()
{
    var userNetworkInfomation = _networkChecker.IsLocal(HttpContext);
    return View(userNetworkInfomation);
}

Anyone know what I'm doing wrong to check whether the user is on the local network or not? Thank you :)

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Dan
  • 21
  • 3

0 Answers0