问题
I builded a ASMX Service and in the code that process an Error Log that comes from ELMAH. I add into the database all the fields and I add some more, one is the IP Address of the requested client...
But I can't (please read, don't know how to) get that...
I'm used to
string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == "") ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
But off course here it does not apply because it's not an HTML request but a SOAP request
in ALL_HTTP header I get all this information:
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:440331
HTTP_CONTENT_TYPE:text/xml; charset=utf-8
HTTP_EXPECT:100-continue
HTTP_HOST:www.mydomainname.com
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3074)
HTTP_VSDEBUGGERCAUSALITYDATA:uIDPowJ7s0UYlmhEqcDjxYQT/TQAAAAADlaOOkJ5DUizA7gKLg3YEPur8pFWlaVFmpinSAOBTuIACQAA
HTTP_SOAPACTION:"http://www. mydomainname.com/webservices/ErrorReportService/ReportErrors"
Where and how can I get the Requested IP?
Note that I'm not using any kind of authentication (yet) ... :)
回答1:
You should be able to use HttpContext.Current.Request.UserHostAddress, but this will not be reliable if the requesting machine is behind a proxy server or NAT device. You'll get the address of the proxy server instead.
I want to make it clear - an IP address is not an appropriate unique identifier. It does not identify a computer. At best, it identifies the Network Layer address that one particular network interface on one particular computer is using at one particular time. At a different time, that same network interface could be using a different IP address. At a different point in time, that same computer might have more than one network interface. You just can't tell.
I seem to remember reading that the designers of the TCP/IP protocols deliberately made it clear that an IP address cannot be used as a unique identifier of a computer. Unfortunately, it seems too few people have read that.
来源:https://stackoverflow.com/questions/964659/get-ip-address-of-the-requested-client-soap-message