0

From this and similar questions I find that the source IP address of requests to an ASP.NET WebAPI application can be found in

HttpContext.Current.Request.UserHostAddress;

Or, more generally, the HttpRequest.UserHostAddress property defined in the documentation here. The type of this property is a simple string, which means any methods intending to accept an IP Address from this source will need to check for formatting since any string could be passed in. However, the documentation doesn't indicate what the format would actually be.

Based on the expected usage, I don't predict that precisely limiting to valid IP Addresses as opposed to just correctly formatted ones would be valuable, so this Regex

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

found here would likely be close to all I need assuming this is the correct format, and it is indeed the most common format that I've seen, but if WebAPI can use different delimiters than . or if it supports IPv6 I would like to know that.

Kamil Drakari
  • 201
  • 4
  • 5
  • 1
    Wouldn't simply using [IPAddress.TryParse](https://msdn.microsoft.com/en-us/library/system.net.ipaddress.tryparse(v=vs.110).aspx) solve your issue? It will parse IPv4 as well as IPv6. – Filburt Aug 28 '17 at 16:34
  • @Filburt Aha! I didn't see the IPAddress type. I'll likely just use that, presumably it will continue to support whatever formats WebAPI uses without needing ongoing support. – Kamil Drakari Aug 28 '17 at 16:37

0 Answers0