CSharpFeeds - All your C# feeds in one place.

Sponsors

Friday, July 28, 2006

ClientBlocker 1.1

by Keyvan Nayyeri via Keyvan Nayyeri on 7/28/2006 2:52:07 PM

I modified my ClientBlocker HTTPModule to handle some unexpected manners.  I had a mistake in exception handling (actually I forgot to call a method on exceptions) which lets blocked clients to pass this blocker and see the site but by some small changes it should work correctly now.  Another change is it returns 200 status for response if client is blocked.

void context_BeginRequest(object sender, EventArgs e)

{

    try

    {

        string ClientIP =

            this._Current.Request.UserHostAddress;

 

        CheckIPs objChecker =

            new CheckIPs(this._FilePath);

 

        if (objChecker.CheckIP(ClientIP) == false)

        {

            if (this._CheckService == true)

            {

                // User wants to check by webservice

                XmlDocument MyDoc = new XmlDocument();

                string Query =

                    "http://api.hostip.info/?ip="

                    + ClientIP;

 

                MyDoc.Load(Query);

 

                XmlNamespaceManager NSManager =

                    new XmlNamespaceManager

                    (MyDoc.NameTable);

                NSManager.AddNamespace("hostip",

                    "http://www.hostip.info/api");

                string XPathQuery

                    = "//hostip:countryAbbrev";

                XmlNode Node = MyDoc.SelectSingleNode

                    (XPathQuery, NSManager);

 

                string Country = null;

                if (Node != null)

                {

                    Country = Node.InnerText;

                }// if

 

                if (this._BlockedCountries.Contains(Country)

                    == true)

                {

                    MyReturn(this._Current);

                }// if

            }// if

            else

            {

                MyReturn(this._Current);

            }// else

        }// if

    }// try

    catch

    {

        // On any unexpected error we let visitor

        // to visit website

    }// catch

}// context_BegingRequest

Where MyReturn and ReturnOutput are:

private void MyReturn(HttpContext Context)

 {

    try

    {

        ReturnOutput(Context);

    }

    catch

    {

        // Block, Block, Block!!

        ReturnOutput(Context);

    }

 }// ReturnOutput

 private void ReturnOutput(HttpContext Context)

 {

    Context.Response.StatusCode = 200;

    Context.Response.SuppressContent = true;

    Context.Response.End();

 }// ReturnOutput

If you want to see full source code of main classes and sample configurations, check my old post here.

New version has been written only for ASP.NET 2.0.  You can download it from here.

email it!bookmark it!digg it!

Original Post: ClientBlocker 1.1

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us