I need to get the IP address of the client, so I did the following:
Created a controller and added this action:
[HttpGet("[action]")] public ActionResult IpAddress() { var remoteIpAddress = HttpContext.Connection.RemoteIpAddress; if (remoteIpAddress != null) return Ok(remoteIpAddress.ToString()); return new EmptyResult(); }
Added the following script to my
site.js
:window.getIpAddress = () => { return fetch('/api/utils/ipaddress') .then((response) => response.text()) .then((data) => { return data }) }
Set up
Startup.cs
to user controllers too, also adding the following:app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
And finally, added the following to my razor component:
var ipAddress = await jsRuntime.InvokeAsync<string>("getIpAddress").ConfigureAwait(true);
I then publish to IIS and access the app.
However, the IP address is not the client's IP address (actually the value of standard gateway) but that of the machine where IIS is running!
What am I doing wrong?