I'm sure this has been done before, but it was faster to write it than to Google for it. there's some IP addresses that have been bothering me and I don't have access to a firewall or IIS at my ISP, so...
I can upload a text file called blockedips.txt to my site and the changes happen immediately.
9 namespace yourmodulenamehere
10 {
11 public class ipblacklist: ihttpmodule
12 {
13 private eventhandler onbeginrequest;
14
15 public ipblacklist ()
16 {
17 onbeginrequest = new eventhandler (this. handlebeginrequest );
18}
19
20 void ihttpmodule. Dispose ()
21 {
22}
23
24 void ihttpmodule. INIT (httpapplication context)
25 {
26 context. beginrequest + = onbeginrequest;
27}
28
29 const string blockedipskey = "blockedips ";
30 const string blockedipsfile = "siteconfig/blockedips. config ";
31
32 public static stringdictionary getblockedips (httpcontext context)
33 {
34 stringdictionary IPS = (stringdictionary) Context. cache [blockedipskey];
35 if (IPS = NULL)
36 {
37 IPS = getblockedips (getblockedipsfilepathfromcurrentcontext (context ));
38 context. cache. insert (blockedipskey, IPS, new cachedependency (getblockedipsfilepathfromcurrentcontext (context )));
39}
40 return IPs;
41}
42
43 Private Static string blockedipfilename = NULL;
44 private static object blockedipfilenameobject = new object ();
45 public static string getblockedipsfilepathfromcurrentcontext (httpcontext context)
46 {
47 If (blockedipfilename! = NULL)
48 return blockedipfilename;
49 lock (blockedipfilenameobject)
50 {
51 if (blockedipfilename = NULL)
52 {
53 blockedipfilename = context. server. mappath (blockedipsfile );
54}
55}
56 return blockedipfilename;
57}
58
59 Public static stringdictionary getblockedips (string configpath)
60 {
61 stringdictionary retval = new stringdictionary ();
62 using (streamreader sr = new streamreader (configpath ))
63 {
64 string line;
65 while (line = Sr. Readline ())! = NULL)
66 {
67 line = line. Trim ();
68 if (line. length! = 0)
69 {
70 retval. Add (line, null );
71}
72}
73}
74 return retval;
75}
76
77 private void handlebeginrequest (Object sender, eventargs evargs)
78 {
79 httpapplication APP = sender as httpapplication;
80
81 If (app! = NULL)
82 {
83 string ipaddr = app. Context. Request. servervariables ["remote_addr"];
84 If (ipaddr = NULL | ipaddr. Length = 0)
85 {
86 return;
87}
88
89 stringdictionary badips = getblockedips (App. context );
90 if (badips! = NULL & badips. containskey (ipaddr ))
91 {
92 app. Context. response. statuscode = 404;
93 app. Context. response. suppresscontent = true;
94 app. Context. response. End ();
95 return;
96}
97}
98}
99}
100}
And in your web. config:
42 <system. Web>
43
44 <add type = "yourmodulenamehere. ipblacklist, yourassemblynamehere"
45 name = "ipblacklist"/>
46
47 </system. Web> using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web;
Using system. Web. sessionstate;
Namespace statewall
{
Public class stateshield: ihttpmodule
{
Public void Init (httpapplication CTX)
{
CTX. prerequesthandlerexecute + = new eventhandler (runsessionrules );
}
Public void dispose (){}
Private void runsessionrules (Object sender, eventargs E)
{
Httpcontext CTX = httpcontext. Current;
Httpsessionstate session = CTX. Session;
If (CTX. Request. url. localpath = "/logout. aspx ")
{
Session. removeall ();
CTX. response. Write ("all session variables are flushed out! ");
CTX. response. End ();
}
If (CTX. Request. url. localpath! = "/Login. aspx ")
{
Int flag = 0;
Foreach (string key in session. Keys)
{
If (key. tostring () = "login ")
{
If (session [Key]. tostring () = "successful ")
{
Flag = 1;
}
}
}
If (flag = 0)
{
CTX. response. Write ("unauthorized access without session .");
CTX. response. End ();
}
}
}
}
}