I have developed an Angular 8 application that is hosted on Server (i.e 123.234.456.XYZ). This UI application is consuming the.DotNet Core API that is hosted on a different server(i.e 123.234.456.PQR).
There is a middleware in my Core API. Which logs the client IP by using the below code.
IPAddress remoteIP = context.Connection.RemoteIpAddress;
Is there any way to get the application server IP (i.e 123.234.456.XYZ) in my middleware?
Tried the below code: But this code is returnning my API hostname
IServerVariablesFeature serverVariablesFeature = context.Features.Get<IServerVariablesFeature>();
var hosteName = serverVariablesFeature["REMOTE_HOST"];
Log.Debug($"Request from REMOTE_HOST: {hosteName}");
Below code returned No such host is known
string HostName= Dns.GetHostEntry(remoteIP).HostName;
Log.Debug($"Request from HostName: {HostName}");
CodePudding user response:
Technically, your Angular application runs in the browser of the user, so the computer of the user accesses the API directly after it has downloaded the Angular application from the server XYZ. So by default there is no contact from the application server XYZ to the API server PQR.
You can solve this on the infrastructure level with a reverse proxy that runs on the application server, for instance under /api. The application then does not access the API directy, but uses the /api URI to connect to the application server that then routes the requests to the API server. If the middleware does need additional data, you can add headers in the reverse proxy that you use in the middleware on the API server to identify the application server.
As a simpler custom approach you can also add headers to the requests to the API in the Angular application that contain the origin of the Angular application (the URL under which the Angular application is running). The middleware on the API server can then use these headers to identity the instance of the Angular application that sent the request.