2

Actually I have a class called BaseRequest which has a variable called ClientIP, currently the front developer should set this variable, but I want this variable not in the class and after calling the web service I get the user's IP address, is it This can, because the code below gives me the IP address of the server, but I want to get the IP address of the user who called the web service.

BaseRequest:

namespace Api.Models.Controllers
{
    public class BaseRequest
    {
        public string AppId { get; set; }
        public string ClientIP { get; set; } //I want to remove this variable and get the user's IP address directly

    }
}

Utility:

public static string GetClientIp()
{
      try
      {
                string strHostName = System.Net.Dns.GetHostName();
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList
                    .FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);

                return ipAddress?.ToString();
       }
       catch (Exception ex)
       {
          return "10.0.0.0";
       }
            
       //return HttpContext.Current.Connection.RemoteIpAddress?.ToString() ?? "10.0.0.0";
}

That is, the AppId variable should be in the BaseRequest class and the ClientIP variable should be removed, and when the web service is called, it can get the user's IP address with the above function and store it in the database, because in this method, the front-end programmer may Set ClientIP to blank or set a fake or fixed IP.

7
  • System.Net.Dns.GetHostName() tries to get the host name of the server where the code is running. a little research on internet suggest to read this and this Commented Sep 6, 2022 at 6:03
  • If you pay attention to the commented code, I have done these researches and I don't want to ask questions again. I have tried all the ways written in these articles and this happens again. @Chetan Commented Sep 6, 2022 at 6:14
  • Can you explain what happens when you try using any of the suggested approach on those link? How they are not solving your issue? Commented Sep 6, 2022 at 6:16
  • Is there a reverse proxy or load balancer before the server? Commented Sep 6, 2022 at 7:01
  • When I call the desired service directly in PostMan, it registers my IP correctly, but when this service is called through a site, the IP address of the server is given to me. @4L4M1N Commented Sep 6, 2022 at 7:43

1 Answer 1

5

Inside a controller get the client ip address using:

var address = Request.HttpContext.Connection.RemoteIpAddress;

If you are using a reverse proxy then you have to use the Forwarded Headers Middleware. Example:

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

This is because the proxy forwards the request to your app so RemoteIpAdress has the ip address of the machine where the proxy is running. Most proxies usually set X-Forwarded-For header with the originating ip address. This middleware reads X-Forwarded-For header and sets the RemoteIpAdress correctly.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

Sign up to request clarification or add additional context in comments.

9 Comments

I have done this but it did not work
Do you understand the example I wrote?
So are you using a reverse proxy or not? If yes, which one?
My question is, can this method be executed automatically on the web service side without getting the address in the controller client or not?
What does 'automatically' mean? A client has to make an http request to server. The http context of the request contain information like the ip address of the client which sent the request.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.