I'm trying to learn ASP.NET Core and I'm making a WhatismyIP website.
I've searched for a way to get the client IP address and I found HttpContext.GetServerVariable("REMOTE_ADDR") but for some reason it's always returning null.
I've also tried X-Forwarded-For but I've ended up having the same problem.
I have port forwarding on my router so I can access the web site from any network (it doesn't work even when I've tested it in the local network).
This is my code (I'm printing the IP for now to the console to test if the code works):
app.MapGet("/MainPage/", async (Context) => {
string? IP = Context.GetServerVariable("REMOTE_ADDR");
string? IP2 = Context.GetServerVariable("X-Forwarded-For");
Console.WriteLine(IP);
Console.WriteLine(IP2);
await Context.Response.WriteAsync("Hello !");
});
I was expecting it to show the client IP address but it showed nothing (null). Do you have any idea why this is happening?
REMOTE_ADDRshould be available, I think it's only if you use IIS as the webserver. See learn.microsoft.com/en-us/dotnet/api/…