views:

651

answers:

3

I've created an ASP.NET WebService that is to be consumed using ASP.NET Ajax. The WebService is located on the same box and same web application that it is to be used by, so I do not want to allow remote access to this webservice, but have it only respond to requests from localhost.

The Web.Config DOES NOT have a configuration section and therfore does not have httpPost and httpGet turned on. This is fine. However, if I navigate directly to the WebService URL from a remote machine, it still loads and shows me a list of methods. Clicking on the method does give me a message stating that the testing form is not available to remote machines (as intended), but it does list information on how to issue a Soap Request and handle a Soap Response.

Additionally, I believe I'm being scraped by a bot of some sort of just a curious user, because I'm now getting error message in my log such as this...

System.InvalidOperationException: Request format is unrecognized for URL
unexpectedly ending in '/ValidateUsername'.

This happens if you try to issue a GET request (by manipulating the query string) against the service remotely. I'm glad that it's not handling the request as I don't want remote users access to this service, but I would prefer it not throw an error.

How can I lock down the webservice so that it is not available to remote machine, but still available to the local machine as a ScriptService consumably by ASP.NET Ajax?

UPDATE: Okay, here is workable example of what is happening.

WebSite: http://so.weirdwes.dyndns.org/default.aspx

WebService: http://so.weirdwes.dyndns.org/services/services.asmx

Web.Config:

<webServices>
  <protocols>
    <remove name="HttpGet"/>
    <remove name="HttpPost"/>
  </protocols>
</webServices>

The website is consuming the WebService using a ScriptManager tag and ScriptReference. You'll note if you click the button, the web service is called and everything works, even though Post and Get have been removed. This is fine as this is how we want it to work. The issue is this.... http://so.weirdwes.dyndns.org/services/services.asmx/GetRemoteAddr

Server Error in '/' Application.
--------------------------------------------------------------------------------

Request format is unrecognized for URL unexpectedly ending in '/GetRemoteAddr'.

A bot or something is scraping this URL and it's generating errors that we're get notified of. I want to supress this error or block it entirely. If I alter the Web.Config and add the Get and Post protocols back in, this error goes away - but then it allows access to the web service remotely using Get which we don't want.

+1  A: 

Test against the ip address
Request.ServerVariables ["REMOTE_ADDR"]

RHicke
I've thought about that, but I'm not even sure it gets to that a point that I can do any testing before it throws the exception I mentioned above. I can give it a shot though.
WesleyJohnson
+1  A: 

IIS lets you white/black list IP groups in its configuration. Use that to lock it down to localhost. You can also configure your firewall to prevent anyone from hitting that port from outside.

Steven Sudit
+1  A: 

I am kinda confused here.

Is this going to be called by the browser? if it will be, then you should allow remote access.

ps
Hmm, that's a good question. I guess I'm not sure how that works. It's being called via the Web Application and the Web Application itself is publically available. Basically I have a <ScriptManager> tag in my page and that has a <ServiceReference> tag pointing at the .asmx file.Then the webservice is being called in various places using JavaScript from the same web application. So, as I understand it, those requests are sceen as coming from localhost - which I want to allow. But if someone were to access the URL directly, the request would an external IP and then I'd want to deny access.
WesleyJohnson
I am pretty sure it is getting called directly by the browser. One way to make sure is log the ip in your service and access your page from a different machine.
ps
Thanks, that's a good idea. I'll give it a shot.
WesleyJohnson
Okay, I'm editing the original question my findings.
WesleyJohnson
check this out..it might be what you are looking for..http://weblogs.asp.net/rashid/archive/2007/09/20/asp-net-ajax-web-service-security.aspx
ps
ps
Thanks PS, I think the weblog.asp.net thing might be the way to go. It seems, as you've said, that POST and GET really should be enabled for a public facing Web Service like this - and then just use a security measure to block access. It appears if I remove the POST and GET, then it's going to generate errors when people access it and there really isn't a way around it - so I think I'll go your route.Thanks!
WesleyJohnson