原文出处:
http://www.codeproject.com/KB/aspnet/http-module-ip-security.aspx
示例代码下载:
点击下载此文件
因为文章看起来比较容易,我就不翻译了。把核心的东西分享出来就行吧
1.先在web.config里面进行设置
<configuration>
<system.web>
<httpModules>
<add name="SecurityHttpModule" type="SecurityHttpModule"/>
</httpModules>
</system.web>
</configuration>
2.使用下面这个方法
程序代码
/// <summary>
/// HTTP module to restrict access by IP address
/// </summary>
public class SecurityHttpModule : IHttpModule
{
public SecurityHttpModule() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
private void Application_BeginRequest(object source, EventArgs e)
{
HttpContext context = ((HttpApplication)source).Context;
string ipAddress = context.Request.UserHostAddress;
if (!IsValidIpAddress(ipAddress))
{
context.Response.StatusCode = 403; // (Forbidden)
}
}
private bool IsValidIpAddress(string ipAddress)
{
return (ipAddress == "127.0.0.1");
}
public void Dispose() { /* clean up */ }
}
其它:把IP段做成数据集就可以大批量限制了。换成IP段也可以的吧。呵呵。