To get the client’s IP address in an ASP.NET Core API controller,

Private IP Address
var privateIPAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
Public IP Address

string url = “https//atulbaba.com”;
var request = System.Net.WebRequest.Create(url);
var getResponse = request.GetResponse();
var reader = new System.IO.StreamReader(getResponse.GetResponseStream());
var response = reader.ReadToEnd().Trim();

var responseArray = response.Split(new Char[] { ‘:’, ‘<‘ },StringSplitOptions.RemoveEmptyEntries);

var publicIPAddresss = responseArray[6];

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace YourApiNamespace.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class IPController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetIPAddress()
        {
            var privateipAddress = HttpContext.Connection.RemoteIpAddress;
            
            string url = "https//atulbaba.com";
            var request = System.Net.WebRequest.Create(url);
            var getResponse = request.GetResponse();
            var reader = new System.IO.StreamReader(getResponse.GetResponseStream());
            var response = reader.ReadToEnd().Trim();
            var responseArray = response.Split(new Char[] { ':', '<' },StringSplitOptions.RemoveEmptyEntries);
            var publicIPAddresss = responseArray[6];
            
            return Ok(privateipAddress); // you can return accordingly.
        }
    }
}