/* namespace eWebapp private string eWebapp_NoLink_Message = string.Empty; public NoLink() public void ProcessRequest(HttpContext context) error = errorLink(context,out myDomain); if(Empty(eWebapp_NoLink_Message)) if(error) } public bool IsReusable { } } } /// <summary> response.Clear(); response.AddHeader(“Content-Disposition”, “filename=” + file.Name); response.AddHeader(“Content-Length”, file.Length.ToString()); string fileExtension = file.Extension.ToLower(); case “mp3”: case “mpeg”: response.ContentType = “video/mpeg”; case “jpg”: response.ContentType = “image/jpeg”; case “bmp”: response.ContentType = “image/bmp”; case “gif”: response.ContentType = “image/gif”; case “doc”: response.ContentType = “application/msword”; break; response.ContentType = “text/css”; default: response.ContentType = “application/octet-stream”; } response.WriteFile(file.FullName); response.End(); try if(referDomain.IndexOf(myDomainIp) >=0 | referDomain.IndexOf(myDomain)>=0) //检查允许匹配 if(myRegex.IsMatch(myPath)) } myRegex = new Regex(eWebapp_NoLink); } return true; } } } }
*
* 防盗链IHttpHandler
*
*
* 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
* 设置web.config中<appSettings>节以下值
* string eWebapp_NoLink 如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤)
* string eWebapp_AllowLink 如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink)
* bool eWebapp_ AllowOnlyFile 如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true
*
*
* :)以下设置均可省略,设置只是为了增加灵活性与体验
* eWebapp_NoLink_Message 错误信息提示:默认为Link From:域名
* eWebapp_Error_Width 错误信息提示图片宽
* eWebapp_Error_Height 错误信息提示图片高
*
*
*
*
* http://ewebapp.net
*/
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
{
/// <summary>
/// 防盗链IHttpHandler
/// 参考http://www.softat.org/archiver/tid-52114.html
///
/// </summary>
public class NoLink : IHttpHandler
{
private string eWebapp_NoLink = string.Empty;
private string eWebapp_AllowLink = string.Empty;
private bool eWebapp_AllowOnlyFile = true;
private bool error = false;
{
//
// TODO: 在此处添加构造函数逻辑
//
}
{
eWebapp_NoLink_Message = ConfigurationSettings.AppSettings[“eWebapp_NoLink_Message”];
string myDomain = string.Empty;
{
eWebapp_NoLink_Message = “Link from :” + myDomain;
}
{
//Jpg(context.Response,eWebapp_NoLink_Message);
Jpg(context.Response,eWebapp_NoLink_Message);
}
else
{
Real(context.Response,context.Request);
}
{
get
return true;
}
}
/// <summary>
/// 输出错误信息
/// </summary>
/// <param name=”Response”></param>
/// <param name=”_word”></param>
private void Jpg(HttpResponse Response,string _word)
{
int myErrorWidth = _word.Length*15;
int myErrorHeight = 16;
try
{
int _myErrorWidth = Convert.ToInt32(ConfigurationSettings.AppSettings[“eWebapp_Error_Width”]);
if(_myErrorWidth > 0 )
{
myErrorWidth = _myErrorWidth;
}
catch
{
try
{
int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings[“eWebapp_Error_Height”]);
if(_myErrorHeight > 0 )
{
myErrorHeight = _myErrorHeight;
}
}
catch
{
Bitmap Img=null;
Graphics g=null;
MemoryStream ms=null;
Img=new Bitmap(myErrorWidth,myErrorHeight);
g=Graphics.FromImage(Img);
g.Clear(Color.White);
Font f=new Font(“Arial”,9);
SolidBrush s=new SolidBrush(Color.Red);
g.DrawString(_word,f,s,3,3);
ms=new MemoryStream();
Img.Save(ms,ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType=”image/Gif”;
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
/// 输出真实文件
/// </summary>
/// <param name=”response”></param>
/// <param name=”context”></param>
private void Real(HttpResponse response,HttpRequest request)
{
FileInfo file = new System.IO.FileInfo(request.PhysicalPath);
//这里选择输出的文件格式
//可以参考http://ewebapp.cnblogs.com/articles/234756.html增加对更多文件格式的支持.
switch (fileExtension)
{
response.ContentType = “audio/mpeg3”;
break;
break;
break;
break;
break;
case “css”:
break;
break;
}
/// <summary>
/// 确认字符串是否为空
/// </summary>
/// <param name=”_value”></param>
/// <returns></returns>
private bool Empty(string _value)
{
if(_value == null | _value == string.Empty | _value == “”)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 检查是否是非法链接
/// </summary>
/// <param name=”context”></param>
/// <param name=”_myDomain”></param>
/// <returns></returns>
private bool errorLink(HttpContext context,out string _myDomain)
{
HttpResponse response = context.Response;
string myDomain = context.Request.ServerVariables[“SERVER_NAME”];
_myDomain = myDomain ;
string myDomainIp = context.Request.UserHostAddress;
eWebapp_NoLink = ConfigurationSettings.AppSettings[“eWebapp_NoLink”];
eWebapp_AllowLink = ConfigurationSettings.AppSettings[“eWebapp_AllowLink”];
{
eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings[“eWebapp_AllowOnlyFile”]);
}
catch
{
eWebapp_AllowOnlyFile = true;
}
if(context.Request.UrlReferrer != null)
{
//判定referDomain是否存在网站的IP或域名
string referDomain = context.Request.UrlReferrer.AbsoluteUri.Replace(context.Request.UrlReferrer.AbsolutePath,””);
string myPath = context.Request.RawUrl;
{
return false;
}
else
{
//这里使用正则表达对规则进行匹配
try
{
Regex myRegex ;
if(!Empty(eWebapp_AllowLink))
{
myRegex = new Regex(eWebapp_AllowLink);
{
return false;
}
//检查禁止匹配
if(!Empty(eWebapp_NoLink))
{
if(myRegex.IsMatch(myPath))
{
return true;
}
else
{
return false;
}
catch
{
//如果匹配出错,链接错误
return true;
}
}
}
else
{
//是否允许直接访问文件
if(eWebapp_AllowOnlyFile)
{
return false;
}
else
{
return true;
}
}
http://www.cnblogs.com/zhouxujian/archive/2006/10/12/527673.html
图片地址防盗链,通过ihttphandler实现_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 图片地址防盗链,通过ihttphandler实现_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧