Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Best solution for me is to filter IPs in IIS.<br> I've done it and it works properly ... and you do not have to change a single line of code.</p> <p>If you do not have access to IIS, then you can follow Scott Hanselman's <a href="http://www.hanselman.com/blog/AnIPAddressBlockingHttpModuleForASPNETIn9Minutes.aspx" rel="nofollow">suggestion</a> and create a custom <code>HttpModule</code>:</p> <pre><code>namespace YourModuleNameHere { public class IPBlackList : IHttpModule { private EventHandler onBeginRequest; public IPBlackList() { onBeginRequest = new EventHandler(this.HandleBeginRequest); } void IHttpModule.Dispose() { } void IHttpModule.Init(HttpApplication context) { context.BeginRequest += onBeginRequest; } const string BLOCKEDIPSKEY = "blockedips"; const string BLOCKEDIPSFILE = "SiteConfig/blockedips.config"; public static StringDictionary GetBlockedIPs(HttpContext context) { StringDictionary ips = (StringDictionary)context.Cache[BLOCKEDIPSKEY]; if (ips == null) { ips = GetBlockedIPs(GetBlockedIPsFilePathFromCurrentContext(context)); context.Cache.Insert(BLOCKEDIPSKEY, ips, new CacheDependency(GetBlockedIPsFilePathFromCurrentContext(context))); } return ips; } private static string BlockedIPFileName = null; private static object blockedIPFileNameObject = new object(); public static string GetBlockedIPsFilePathFromCurrentContext(HttpContext context) { if (BlockedIPFileName != null) return BlockedIPFileName; lock (blockedIPFileNameObject) { if (BlockedIPFileName == null) { BlockedIPFileName = context.Server.MapPath(BLOCKEDIPSFILE); } } return BlockedIPFileName; } public static StringDictionary GetBlockedIPs(string configPath) { StringDictionary retval = new StringDictionary(); using (StreamReader sr = new StreamReader(configPath)) { String line; while ((line = sr.ReadLine()) != null) { line = line.Trim(); if (line.Length != 0) { retval.Add(line, null); } } } return retval; } private void HandleBeginRequest(object sender, EventArgs evargs) { HttpApplication app = sender as HttpApplication; if (app != null) { string IPAddr = app.Context.Request.ServerVariables["REMOTE_ADDR"]; if (IPAddr == null || IPAddr.Length == 0) { return; } StringDictionary badIPs = GetBlockedIPs(app.Context); if (badIPs != null &amp;&amp; badIPs.ContainsKey(IPAddr)) { app.Context.Response.StatusCode = 404; app.Context.Response.SuppressContent = true; app.Context.Response.End(); return; } } } } } </code></pre> <p>and use it in your <code>web.config</code>:</p> <pre><code>&lt;system.web&gt; &lt;httpModules&gt; &lt;add type = "YourModuleNameHere.IPBlackList, YourAssemblyNameHere" name="IPBlackList" /&gt; &lt;/httpModules&gt; &lt;/system.web&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload