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.