In web forms if I needed to obtain the website server's IP address (say for logging) I could obtain it from request.ServerVariables["LOCAL_ADDR"]
How can it be obtain in Asp.Net Core when running in Kestral behind IIS or IIS Express?
Client IP address can be retrieved via HttpContext. Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property RemoteIpAddress is the client IP address.
You can use HttpContext.Features.Get<IHttpConnectionFeature>() - docs:
var httpConnectionFeature = httpContext.Features.Get<IHttpConnectionFeature>();
var localIpAddress = httpConnectionFeature?.LocalIpAddress;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With