1

I'm trying to implement IP Address whitelisting using owin middleware. When I try to get client ip address from the request (context.Request.RemoteIpAddress) I'm sometimes getting a link-local IPv6 address(example IPv6 with %13 at the end) and I sometimes get the IPv4 address

I'm running the web application locally and the api is also hosted locally, which property in owin context request will give me the exact proper IpAddress (IPv4 or IPv6 but not link local ip address)?

user3861992
  • 289
  • 3
  • 13
  • Both the IPv4 and IPv6 addresses are correct - it depends on how a client chooses to establish the connection. If you only want to support IPv4, which I don't recommend, you could edit the site's Bindings in IIS so that it only listens on IPv4 addresses. – AlwaysLearning Aug 09 '19 at 23:31

2 Answers2

1

try

string ipAddress1 = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
string ipAddress2 = Request.ServerVariables["REMOTE_ADDR"];

ipAddress = string.IsNullOrEmpty(ipAddress1) ? ipAddress2 : ipAddress1;

ViewBag.IP = ipAddress;

Grettings

Ransomware0
  • 427
  • 3
  • 10
0

To get IPv6 or IPv4 use :

var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;

To get IPv4 use:

context.HttpContext.Connection.RemoteIpAddress.MapToIPv4();

To get IPv6 use:

context.HttpContext.Connection.RemoteIpAddress.MapToIPv6();

Also, you can try HTTP_X_FORWARDED_FOR : Solution

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35