user687554
user687554

Reputation: 11131

Detecting Request from Localhost in C#

I have a challenge and I believe there is a developer smarter than me that can provide some insight.

I have a web service. This web service is written with ASP.NET MVC in C#. I want to allow developers to call this web service. When developers are writing code, I recognize that web apps typically run from localhost. When they call this service, I want to be able to identify if the request is coming from localhost. However, if I look at the IP address, its the IP address of their machine.

Is there a way for me to even do this? Clearly Request.IsLocal won't work as my web service is running on an entirely different machine.

Upvotes: 0

Views: 1702

Answers (3)

bmm6o
bmm6o

Reputation: 6515

When you call a web service, the browser usually passes the page in the Referer header. So you can check if that value starts with "http://localhost". Virtually anything in an http request can be forged (including this), so be careful what kind of decisions you make based on this data.

Upvotes: 1

Frazell Thomas
Frazell Thomas

Reputation: 6111

How will you then define local (from the perspective of your service)? You'd be better off setting up a development service on a different API end point instead of attempting to guess this.

All production level API calls can go to something like api.yourservice.com with all development level requests coming in via dev.yourservice.com.

You can then have two separate services or have your service read the URL being requested and differentiate based on this.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245429

Without passing some additional data along with the request from the app, there's not going to be any way for you to know.

You'll only be able to get the IP address or Host name that was used to make the request to your Web Service and it sounds like you want to be able to find the Host Name (localhost) that was used to make the request to the app (which then triggers the call to the Web Service).

Upvotes: 0

Related Questions