Global.asax或IHttpModule实现屏蔽ip和图片防盗链

   
     
protected void Application_BeginRequest( object sender, EventArgs e)
{
// 屏蔽ip
// if (HttpContext.Current.Request.UserHostAddress == "127.0.0.1")
// {
// HttpContext.Current.Response.Write("你已被屏蔽");
// HttpContext.Current.Response.End();
// }
// 图片防盗链,只要是图片请求,并且请求地址不是本服务器都将图片换位下面的盗链图片
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith( " .jpg " ) && HttpContext.Current.Request.UrlReferrer.Host != " localhost:2535 " )
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath(
" ~/imgs/%D7%EE%D6%D5%BB%C3%CF%EB%A2%F7-%CA%A5%CD%AF%BD%B5%C1%D920080427084745.jpg " ));
}
}

你可能感兴趣的:(Module)