Sep 4, 2016

How to get remote IP address in Asp.Net Core Web API


In Asp.Net Core Web API to get Remote Client, IP Address is changed to the previous version of Asp.Net
.

In Core introduced new library for Http request and response.

Require the following namespace to add.
using Microsoft.AspNetCore.Http.Features;
Once, you added namespace then just need to add following a line of code for capture the Remote Client IP Address.
HttpContext.Features.Get()?.RemoteIpAddress?.ToString();
Once more thing I want to explain about null handle in Core using "?".

So you can see in the above line how to handle the null object.

Note: When you try to run the application from the local system so above line of code return the result "::1" but it will work once you deploy your application somewhere.

Hope you getting the desired result and save your time.

No comments:

Post a Comment