Monday, April 13, 2009

How to block a particular I.P. from accessing your site using HttpModule

This sample can be used to block a particular ipaddress from accessing resources on your website.We can retrieve information regarding client browser and client machine by making use of the Request object.Asp.Net uses a series of modules to get the work done for you.For a list of available modules in ASP.Net you can refer machine.config file present in %windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG folder.You can either use these modules or can create your custom module for the same.

HTTPMODULE:
An HttpModule is an assembly that implements the IHttpModule interface and handles events. ASP.NET includes a set of HttpModules that can be used by your application.. For example, the SessionStateModule is provided by ASP.NET to supply session state services to an application. Custom HttpModules can be created to respond to either ASP.NET events or user events..

The general process for writing an HttpModule is:

Implement the IHttpModule interface.
Handle the Init method and register for the events you need.
Handle the events.
Optionally implement the Dispose method if you have to do cleanup.
Register the module in Web.config.

Namespace for Httpmodule is: System.Web
Assembly:System.Web.dll
Let us go through the example step by step.. Step1: open visual studio> File > New > Website > chose ur language as c# as we are using in tour application> Select the location> Name the website Step2: Right click and add new item, select class and rename it as ipaddress.cs.. step3:




step 4:
Add these following lines in your code..

Type Your own ipaddress in ipadd to check the application...


step5: Adding Httpmodules in your Web.Config Type should be in namespace.classname format..




I hope you people like this application...

1 comment: