Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Asp.Net Core core how to I get the web site's IP address?

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?

like image 999
RonC Avatar asked Oct 31 '16 17:10

RonC


People also ask

How do I find client IP address and location in ASP.NET Core?

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.


1 Answers

You can use HttpContext.Features.Get<IHttpConnectionFeature>() - docs:

var httpConnectionFeature = httpContext.Features.Get<IHttpConnectionFeature>();
var localIpAddress = httpConnectionFeature?.LocalIpAddress;
like image 51
tmg Avatar answered Oct 12 '22 17:10

tmg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!