All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
理论:论C#变得越来越臃肿是不可避免的_.net资料_编程技术
PHPUnit袖珍指南之命令行测试工具_php资料_编程技术
Linux系统Apache用户授权和访问控制_php资料_编程技术
安全攻略:PHP脚本木马的高级防范_php资料_编程技术
sql server2005设置自动备份全过程_[SQL Server教程]
ASP.NET开发系列之在用户控件中添加事件_[Asp.Net教程]
ASP.NET实现简单的验证码_[Asp.Net教程]
PHP技巧:过滤在线编辑器产生的不安全html代码_[PHP教程]
技巧文集:PHP如何禁止图片文件的被盗链_php资料_编程技术
初学者来看:了解什么是PHP和PHP的功能_php资料_编程技术
Reading number is top 10 pictures
西班牙山村小景2
Discharge accidentally Actresses by the breast1
Forced sex girl living abroad1
我国房地产真相
运动的范冰冰3
美女浴室写真2
Born After 90 Beijing sports university campus flower photos4
传几朵花
黑社会大哥相亲
赵惟依写真1
Download software ranking
尖东毒玫瑰A
终极变速大师Speeder3.26
Eclipse 4.2.1 For Win32
Unix video tutorial20
Desire a peach blossom
jdk1.5
The cock of the Grosvenor LTD handsome
Tram sex maniac 2 (H) rar bag7
I'm come from Beijing1
卡丁车单机版
delv published in(发表于) 2014/1/10 6:30:59 Edit(编辑)
图片地址防盗链,通过IHttpHandler实现_[Asp.Net教程]

图片地址防盗链,通过IHttpHandler实现_[Asp.Net教程]

图片地址防盗链,通过IHttpHandler实现_[Asp.Net教程]

/*
*
* 防盗链IHttpHandler
*
*
* 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
* 设置web.config中节以下值
* 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;


namespace eWebapp
{
///


/// 防盗链IHttpHandler
/// 参考http://www.softat.org/archiver/tid-52114.html
///
///

public class NoLink : IHttpHandler
{
private string eWebapp_NoLink = string.Empty;
private string eWebapp_AllowLink = string.Empty;
private bool eWebapp_AllowOnlyFile = true;


private string eWebapp_NoLink_Message = string.Empty;
private bool error = false;


public NoLink()
{
//
// TOD 在此处添加构造函数逻辑
//
}


public void ProcessRequest(HttpContext context)
{
eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"];


string myDomain = string.Empty;


error = errorLink(context,out myDomain);


if(Empty(eWebapp_NoLink_Message))
{
eWebapp_NoLink_Message = "Link from :" + myDomain;
}


if(error)
{
//Jpg(context.Response,eWebapp_NoLink_Message);
Jpg(context.Response,eWebapp_NoLink_Message);
}
else
{
Real(context.Response,context.Request);
}


}


public bool IsReusable
{
get


{
return true;
}
}



///


/// 输出错误信息
///

///
///
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();
}


///


/// 输出真实文件
///

///
///
private void Real(HttpResponse response,HttpRequest request)
{
FileInfo file = new System.IO.FileInfo(request.PhysicalPath);


response.Clear();


response.AddHeader("Content-Disposition", "filename=" + file.Name);


response.AddHeader("Content-Length", file.Length.ToString());


string fileExtension = file.Extension.ToLower();



//这里选择输出的文件格式
//可以参考http://ewebapp.cnblogs.com/articles/234756.html增加对更多文件格式的支持.



switch (fileExtension)
{


case "mp3":
response.ContentType = "audio/mpeg3";
break;


case "mpeg":


response.ContentType = "video/mpeg";
break;


case "jpg":


response.ContentType = "image/jpeg";
break;


case "bmp":


response.ContentType = "image/bmp";
break;


case "gif":


response.ContentType = "image/gif";
break;


case "doc":


response.ContentType = "application/msword";


break;
case "css":


response.ContentType = "text/css";
break;


default:


response.ContentType = "application/octet-stream";
break;


}


response.WriteFile(file.FullName);


response.End();
}



///


/// 确认字符串是否为空
///

///
///
private bool Empty(string _value)
{
if(_value == null | _value == string.Empty | _value == "")
{
return true;
}
else
{
return false;
}
}



///


/// 检查是否是非法链接
///

///
///
///
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"];


try
{
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;


if(referDomain.IndexOf(myDomainIp) >=0 | referDomain.IndexOf(myDomain)>=0)
{
return false;
}
else
{
//这里使用正则表达对规则进行匹配
try
{
Regex myRegex ;


//检查允许匹配
if(!Empty(eWebapp_AllowLink))
{

myRegex = new Regex(eWebapp_AllowLink);


if(myRegex.IsMatch(myPath))
{
return false;
}


}



//检查禁止匹配
if(!Empty(eWebapp_NoLink))
{


myRegex = new Regex(eWebapp_NoLink);
if(myRegex.IsMatch(myPath))
{
return true;
}
else
{
return false;
}


}


return true;


}
catch
{
//如果匹配出错,链接错误
return true;
}
}
}
else
{
//是否允许直接访问文件
if(eWebapp_AllowOnlyFile)
{
return false;
}
else
{
return true;
}
}


}


}


}


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.