How to Stop Leeching in ASP.Net
1: using System;
2: using System.Web;
4: namespace Site.HTTPModules
6: public class AntiLeech:IHttpModule
8: public void Dispose()
13: public void Init(HttpApplication context)
15: context.BeginRequest += new EventHandler(context_BeginRequest);
18: void context_BeginRequest(object sender, EventArgs e)
20: if(((HttpApplication)sender).Context.Request.UrlReferrer.Host !=((HttpApplication)sender).Context.Request.Url.Host)
22: ((HttpApplication)sender).Context.Response.Status="Thanks for leeching";
All this code does, is checks the referring host of the request being made and if it is not the same as the host for the file requested, it returns a 401 unauthorized code along with a nice little message. This, however, can cause issues if people are using software to translate your site so you may want to keep a list of files you think are being leeched and only block those.
Now you may try compiling that code into your website and you'll notice that it doesn't work. This is because you haven't added the module to the web.config file yet. All you need to do is find the httpModules section and add in the following line:
1: <add name="AntiLeech" type="Site.HttpModules.AntiLeech,Site"/>
Where Site is the default namespace for your site. You'll also need to change the namespace of the c# code above.
You could of course modify this code so that certain locations/hosts can view your content, certain files/file types can pass through without any issues, etc. But the code above should get you started. So look at the code, leave feedback, and happy coding.