How to be sure a http request is local when using HttpListener

928 Views Asked by At

I'm using HttpListener. However, I only want to process requests that come locally, not from another machine/server.

How would I programmatically verify if an incoming request is local for sure? Would I need to use some of the HttpListenerRequest members?

2

There are 2 best solutions below

0
On BEST ANSWER

Check with RemoteEndPoint property if the remote address of the request equals 127.0.0.1.

4
On

The HttpListenerRequest has a property IsLocal which should also provide this information. However, it is returning false on my computer. Reflecting into the source code, it appears that this is because the IsLocal property uses the code:

    return this.LocalEndPoint.Address == this.RemoteEndPoint.Address;

when perhaps it should use the code in the (inaccessible) InternalIsLocal property which is:

    return this.LocalEndPoint.Address.Equals(this.RemoteEndPoint.Address);

(This expression returns true for my situation). Using this approach might be preferable so that your code is not dependent on an IPv4 address as the transition is made to IPv6.