Forward client IP from ASP.NET Web API to .NET Core API hosted in Azure

I have an Angular app that makes requests to a .NET 4.6.2 Web MVC Controller and the code inside the controller makes a requests with a HttpClient to a .NET Core 3.1 API. All apps is hosted in Azure.

So Angular -> .NET 4.6.2 -> .NET Core 3.1 API.

I want to get the originating client IP in the .NET Core app. Requests that are sent directly from the client to .NET Core app is working fine by reading HttpContext.Connection.RemoteIpAddress after I added the environment variable ASPNETCORE_FORWARDEDHEADERS_ENABLED = true in Azure. But not when going through the .NET 4.6.2 app, in that case the IP is an Azure IP and not the clients IP.

What is the best way to forward the client ip information from .NET 4.6.2 app to the NET Core 3.1 app?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could read the client’s IP address in the .NET 4.6.2 app and send it to the .NET Core app using custom HTTP header (something like X-CLIENTIP).
On the .NET Core app first try to get the custom header. If it doesn’t exist (which means the call came directly from the client), read it from HttpContext.Connection.RemoteIpAddress


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x