English 中文(简体)
ASP.NET WebService deny remote access
原标题:

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.

最佳回答

I am kinda confused here.

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

问题回答

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签