如何使用http模块阻止ip范围?
问题描述:
如何用c#使用http模块阻止Ip范围。我有一个简单的解决方案,但其单一的IP和如何我可以读取XML文件中的IP HTTP模块,这样,如果一个IP有没有在该文件将被阻止。这里是我的代码如何使用http模块阻止ip范围?
public class MyHandler :IHttpModule
{
public MyHandler(){}
public bool IsReusable
{
get { return false; }
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
private void Application_BeginRequest(object source, EventArgs e)
{
HttpContext context = (source as HttpApplication).Context;
List<string> BlackListIp = new List<string>();
BlackListIp.Add("127.0.0.1");
if (BlackListIp.Contains(context.Request.UserHostAddress))
{
context.Response.Write("<h1 style='color:red;'>Your IP is Blocked</h1>");
}
}
public void Dispose()
{
}
}
答
只需添加app_data文件夹内的xml文件并使用XDocument读取它。 您可以使用具有FileDependency的缓存来提高性能。
看来您已经有了阻止IP的代码。你的问题是如何从一个http模块中的xml文件中读取的? – 2012-04-27 22:49:30
是的,我有代码@JohnW – HBK 2012-04-27 22:58:24