I have a JavaScript ajax call to my web api controller. I need to get their wan ip address when they make that call.
I have seen many examples for MVC but I am using asp.net core and the methods there do not work. The value(s) are always null.
This is js:
$.ajax({
url: "/my server ip",
type: "POST",
contentType: "application/json; charset=utf-8",
headers: { 'Content-Type': 'application/json' },
success: function (data) {
$("#DeviceIp").html(data.ip + " " + data.wanIp);
}
});
my api:
IActionContextAccessor accessor
_accessor = accessor;
[HttpPost]
[Route("Account/GetDeviceIp")]
public IpStats GetDeviceIp()
{
var WanIp = _accessor.ActionContext.HttpContext.Request.Headers.ToString();
}
NB
I should have said that this:
_accessor.ActionContext.HttpContext.Connection.RemoteIpAddress.ToString()
gave me null. The other method was not null but no wanip address on that object...
ALSO
I am using nginx and not IIS