1

I am calling a Server with Post method and some data. The Server needs to have the Caller's IP Address or any sort of Identity. I am using $.Post() method of JQuery to Send in my POST request from my Machine.

At he Server End, If I get the HttpRequestMessage.Referrer.Host name its showing as "localhost". I just want to know how to iclude my IP information in the referrer or Origing headers of my Http request or opening my CLient Web Application which sends the Post request using some IP is the Only go? PFB my Java Script for this..

    function submitValues() {
    var accno = $("#accno").val();
    var amt = $("#amount").val();
    var modifiedacctno = Math.floor((Math.random() * 100000) + 1);
    var modifiedAmt = Math.floor((Math.random() * 10000) + 1);

    var posting=$.post("http://192.168.2.3:50063/api/Transaction", { ToAccountNumber: modifiedacctno, amount: modifiedAmt }, null, "html")
  posting.done(function (data) {
      console.log("Data Loaded: " + data);
      $("#result").empty().append(data);
  });
}
Nate
  • 30,286
  • 23
  • 113
  • 184

1 Answers1

0

Even if you were able to somehow get the callers IP address via javascript, often times a client, will not know its own IP address. This is because most people are behind NAT so their computer thinks they are on IP Address 192.168.25.53 but their public address is most likely different than that.

The way to get the client's IP address, is on the server. The correct way to do this is to use the Request object, and more specifically the UserHostAddress property.

var remoteUserIp = Request.UserHostAddress;
// do your processing with remoteUserIp here

Upon further review, this appears to not be a good way to do this for a WebAPI, please see the answer linked by @Kiran -- https://stackoverflow.com/a/9571292/1184056

Community
  • 1
  • 1
Nate
  • 30,286
  • 23
  • 113
  • 184
  • Thats Right, I have my POST Method SIgnature in ASP.NET Web API as ([FromBody] Details value, HttpRequestMessage Msg) but when I change the HttRequest Message to HttpRequest, My Client started throwing internal Server Error. Not sure why this Happens, but do you see anyother way through the HttpRequestMessage Class and get the Client IP Address? – Sri Krishna Mallikarjuna Jun 18 '14 at 22:18
  • Thanks a Ton Kiran.....It worked..You saved my Day..Wondered why its is so roundabout to get the requestor's IP Address. But Never mind Thanks once again – Sri Krishna Mallikarjuna Jun 18 '14 at 22:40