4

I have a webservice hosted in my IIS... I need to find out the clientIP address when the client use my service directly

like http://MyIpAddress/MyApplication/MyWebServiceClass.asmx

and is it possible to read file from the client machine? If so how can I do it?

user824910
  • 1,067
  • 6
  • 16
  • 38
  • Possible duplicate of http://stackoverflow.com/questions/492952/how-to-retrieve-pc-name-of-client-from-c-webservice – Quasdunk Aug 08 '11 at 12:15

3 Answers3

8

You should have a plain old HTTP Context at your disposal in ASMX:

        HttpContext.Current.Request.UserHostAddress

Also re: "Is it possible to read a file from the client machine" - this all depends on your implementation. If you're making a web service for your intranet and you work in a small(ish) business environment, you probably can given the proper planning w/ your network guy (not advocating this as a good idea, just a possibility).

To further elaborate, if you are in your small office environment and you get a request from 192.168.1.55 and you know that every client machine in your network has a lastLoginData.txt file in the C drive, AND you have the appropriate configurations for UNC access to the client by the machine hosting the service, getting at "\\" + ip + "\c$\lastLoginData.txt" would be possible. You'd be creating a potentially horrible security issue for yourself, but it'd be possible.

In most normal cicumstances though, no, you will not have access to client disk from the Web Service - some sort of upload will likely have to occur first.

Brian
  • 2,772
  • 15
  • 12
  • Is there any way to get read the file on knowing the address? – user824910 Aug 08 '11 at 12:18
  • Side note: Use IPAddress.Parse to resolve the string into an IPAddress object. Then you can do stuff like. Equals(IPAddress.Loopback) to test if the request is coming from the local machine, etc. – user169771 May 14 '15 at 15:13
4

Try calling

Request.UserHostAddress

HttpRequest.UserHostAddress Property

With regards to accessing a file from the client, this would need be achieved by first uploading the file to the server.

Checkout the following on uploading files to a web service:

ASMX file upload

Create a simple file transfer Web service with .NET

Community
  • 1
  • 1
jdavies
  • 12,784
  • 3
  • 33
  • 33
0
        String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (string.IsNullOrEmpty(ip))
        {
            ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        else
        {
            ip=ip.Split(',')[0];
        }

        return ip;
SATHISH kumar
  • 31
  • 1
  • 11