Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top