How to delete / cancel a request in ASP.NET?

I am trying to understand how to refuse a request, I am mainly trying to implement a blocked list of IP addresses in my application to block spammers, and I do not want to return any response (just ignore the request), is this possible in ASP.NET?

Edit: some of the answers indicate that I can add them to the firewall, although this will certainly be better than it does not fit in my case. In short, I am adding a moderation section to my site where moderators will check messages waiting for spam moderation (filtered by a spam filler), I want the sender's IP address of some mail to be added to the list of blocked IP addresses when the message is marked as spam moderator, so I did it in the application.

Edit: Calling Response.End () returns the answer to the user (even if it is empty), the whole purpose of my question was not to return any answer. Is this possible (even out of curiosity)? There is also Response.Close (), which closes the socket, but it sends a notification (in TCP / IP) when it does, I just ignore it if it has never been received (i.e., sends nothing to the user )

thanks

+5
source share
9 answers

For this type of thing, I would go with an HTTP module , in contrast, with a handler or page.

? HTTP- . . , , .

.

+4

,

Response.Clear();
Response.End();
+3

, . IP- IP- , , - .

+2

EDIT: HTTPModule , . . http://www.kowitz.net/archive/2006/03/08/ihttpmodule-vs-ihttphandler.aspx


, httphandler, - , .

Psudo -

class IgnoreHandler : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        context.Response.StatusCode = 401;
        context.Response.Status = "Unauthorized";
        context.Response.End();
    }

    #endregion
}

, , HTTP 200 (OK) , , StatusCode Status.

web.config

<httpHandlers>
    <add verb="*" 
         path="*" 
         validate="false" 
         type="MyNamespace.IgnoreHandler, MyAssembly" />
</httpHandlers>
+2

, "", http. , , /url not found :

throw new HttpException(404, "File not found - " + Request.AppRelativeCurrentExecutionFilePath);

, , .

0

, , .

0

AFAIK there is no way to completely refuse a request without an answer. You can save the request spinning in ASP.NET, with any thread-changing technology you like, but you are not making life better for your server in this way.

The best way I know to achieve what you want is to have a traffic manager, for example ZXTM close the request before it gets to the web server at all.

0
source

Here you go:

public class RequestDropper : IHttpModule
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += Context_BeginRequest;
    }

    public void Context_BeginRequest(object sender, EventArgs e)
    {
        var request = ((HttpApplication)sender).Context.Request;

        if (todayIAmNotInAMoodToProcessRequests)
        {
            request.Abort();
        }
    }
}

Hope this saves you some time.

0
source

All Articles