asp.net 防盗链

项目结构:

 

 

在这里我们可以实现图片、样式表、jS...等等文件的盗链

1.首先在项目中添加 Global.asax  全局文件

2.添加盗链判断处理类:HotLinkFactory.cs

3.自定义Global 中的方法实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Img_Anti_hotlinking
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}

protected void Session_Start(object sender, EventArgs e)
{
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
//1.判断远程访问的主机是否需要验证(如果是自己公司内部的网站共用图片,当然就不需要禁用图片链接了)

//获得发送请求页面的服务器主机名称,本机为:localhost,如果是百度盗链了我们网站中的文件则访问的主机名则为:www.baidu.com
if (HttpContext.Current.Request.UrlReferrer != null)
{
string _HostName = HttpContext.Current.Request.UrlReferrer.Host;

//判断主机是否为可信任主机,如果为信任主机则允许其盗链文件

//如果需要查看盗链效果,请在HotLinkFactory类中将localhost从信任主机中移除
if (!HotLinkFactory.Instance.IsLocalServer(_HostName))
{
string UrlPath = HttpContext.Current.Request.Url.AbsolutePath;
string FileType = UrlPath.Substring(UrlPath.LastIndexOf('.'));

if (HotLinkFactory.Instance.IsNoAuthFile(FileType))
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/Images/qw.png"));
HttpContext.Current.Response.End();
}
}
else
{
//为信任主机,不做操作
}
}
}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}

protected void Application_Error(object sender, EventArgs e)
{
}

protected void Session_End(object sender, EventArgs e)
{

}

protected void Application_End(object sender, EventArgs e)
{
}
}
}

 

4.HotLinkFactory.cs 实现代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Img_Anti_hotlinking
{
///<summary>
/// 防盗链处理类
///</summary>
public class HotLinkFactory
{
private static HotLinkFactory linkfactory = new HotLinkFactory();
public static HotLinkFactory Instance
{
get
{
return linkfactory;
}
}

///<summary>
/// 判断访问的服务器是否为可信任的服务器
///</summary>
///<param name="_serverName">服务器名称</param>
///<returns></returns>
public bool IsLocalServer(string _serverName)
{
bool bolLocal = false;
//我们可以将信任的服务器地址都写入到一个XML文件中,在判断时从XML文件中读取出所有的可信任的服务器地址
//此处为了方便演示,创建一个虚拟的信任服务器列表
List<string> localserverList = new List<string>();
localserverList.Add("localhost"); //要查看测试效果时,可将此项移除
localserverList.Add("www.qq.com");
//......具体的可以根据情况从配置文件中读取添加

if (localserverList.Contains(_serverName))
{
bolLocal = true;
}
return bolLocal;
}

///<summary>
/// 判断其访问的是否为未授权使用的文件,如果是,则禁止其使用
///</summary>
///<param name="_fileType">文件后缀</param>
///<returns></returns>
public bool IsNoAuthFile(string _fileType)
{
bool IsNoAuth = true;
_fileType = _fileType.ToLower();
switch (_fileType)
{
case ".jpg":
IsNoAuth = true;
break;
case ".png":
IsNoAuth = true;
break;
case ".bmp":
IsNoAuth = true;
break;
case ".gif":
IsNoAuth = true;
break;
case ".tif":
IsNoAuth = true;
break;
case ".pdf":
IsNoAuth = true;
break;
case ".doc":
IsNoAuth = true;
break;
case ".xls":
IsNoAuth = true;
break;
case ".css":
IsNoAuth = true;
break;
case ".js":
IsNoAuth = true;
break;
}
return IsNoAuth;
}
}
}

 

因为本地主机已添加到信任列表,运行效果如下:

如果在  HotLinkFactory.cs 中将localhost从信任列表中移除:

     ///<summary>
/// 判断访问的服务器是否为可信任的服务器
///</summary>
///<param name="_serverName">服务器名称</param>
///<returns></returns>
public bool IsLocalServer(string _serverName)
{
bool bolLocal = false;
//我们可以将信任的服务器地址都写入到一个XML文件中,在判断时从XML文件中读取出所有的可信任的服务器地址
//此处为了方便演示,创建一个虚拟的信任服务器列表
List<string> localserverList = new List<string>();
//localserverList.Add("localhost"); //要查看测试效果时,可将此项移除
localserverList.Add("www.qq.com");
//......具体的可以根据情况从配置文件中读取添加

if (localserverList.Contains(_serverName))
{
bolLocal = true;
}
return bolLocal;
}

再次运行程序,我们即会见到盗链提示的效果:

 

   源码下载 

 

posted @ 2011-11-09 19:04  小罗》  阅读(381)  评论(0)    收藏  举报