当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.net中实现基于UrlRewrite的防盗链功能

ASP.net中实现基于UrlRewrite的防盗链功能

2017年12月12日  | 移动技术网IT编程  | 我要评论

随笔小记,2011年四川高考理综,最新致富信息

asp.net中最快实现urlrewrite的方法这篇文章中说了如何做urlrewrite,那只是一个最简单的应用

其实利用urlrewrite与iis的设置我们可以实现简单而有效的防盗链功能。

假设你的站点有一个文件:web.rar,你希望只有具有某些特定域名的来源地址或是已经登陆的用户才能访问,这时就得用到防盗链功能,在asp时代,我们需要借助第三方组件来完成这个效果,但是在asp.net中我们可直接利用context.rewritepath来实现了。

下载配置文件:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<download>
<checktype>1</checktype>
<cookiesname>username</cookiesname>
<urlpattern>
<![cdata[//(.+?)/.rar/b]]>
</urlpattern>
<urlreplace>
<![cdata[default.aspx?d=$1.rar]]>
</urlreplace>
<allowhost>
<![cdata[127.0.0.1]]>
</allowhost>
</download>

说明:

checktype:要求验证的类型(1:只验证合法的域名,2:只验证是否有cookies,3:同时验证域名与cookies)
cookiesname:要验证的cookies名称,可为空。
urlpattern:请求的url格式。
urlreplace:当下载无效时转向的url格式。
allowhost:允许的来源域名。

global.aspx中的配置:
复制代码 代码如下:

void application_beginrequest(object sender, eventargs e)
{
bool isallowdomain = false;
bool islogin = false;
string cookiesname = "username", allowhost, referrerhost="";
int checktype = 1;
bool allowdown = false;
string[] allowhostarr;
string urlpattern = "", urlreplace = "";
string[] pattern, replace;
string configfile = configurationmanager.appsettings["downloadconfig"];
if (configfile != "")
{
try
{
system.xml.xmldatadocument xdconfig = new system.xml.xmldatadocument();
xdconfig.load(appdomain.currentdomain.basedirectory + @"/" + configfile);
if (xdconfig.selectsinglenode("download/checktype").innertext != "")
{
checktype = int.parse(xdconfig.selectsinglenode("download/checktype").innertext);
}
if (xdconfig.selectsinglenode("download/cookiesname").innertext != "")
{
cookiesname = xdconfig.selectsinglenode("download/cookiesname").innertext;
}
allowhost = xdconfig.selectsinglenode("download/allowhost ").innertext;
allowhostarr = allowhost.split('|');
urlpattern = xdconfig.selectsinglenode("download/urlpattern").innertext;
urlreplace = xdconfig.selectsinglenode("download/urlreplace").innertext;
pattern = urlpattern.split('@');
replace = urlreplace.split('@');
if (cookiesname == "") cookiesname = "username";
islogin = false.equals(request.cookies[cookiesname] == null || request.cookies[cookiesname].value == "");
if (request.urlreferrer != null) referrerhost = request.urlreferrer.host.tostring();
if (allowhostarr.length < 1)
{
isallowdomain = true;
}
else
{
for (int hosti = 0; hosti < allowhostarr.length - 1; hosti++)
{
if (allowhostarr[hosti].tolower() == referrerhost.tolower())
{
isallowdomain = true;
break;
}
}
}
switch (checktype)
{
case 1:
allowdown = true.equals(isallowdomain);
break;
case 2:
allowdown = islogin;
break;
case 3:
allowdown = true.equals(isallowdomain && islogin);
break;
}
if (allowdown == false)
{
string oldurl = httpcontext.current.request.rawurl;
string newurl = oldurl;
for (int iii = 0; iii < pattern.length; iii++)
{
if (regex.ismatch(oldurl, pattern[iii], regexoptions.ignorecase | regexoptions.compiled))
{
newurl = regex.replace(oldurl, pattern[iii], replace[iii], regexoptions.compiled | regexoptions.ignorecase);
oldurl = newurl;
}
}
this.context.rewritepath(newurl);
}
}
catch
{
}
}
}

web.config中的配置:
复制代码 代码如下:

<appsettings>
<add key="downloadconfig" value="download.config"/>
</appsettings>

iis中的配置:

可执行文件填入:c:/windows/microsoft.net/framework/v2.0.50727/aspnet_isapi.dll(视实际情况变动,与.aspx的一样就成)

记得把那个:检查文件是否存在 前的勾去掉。

你可为任何你想要防盗链的文件加上这个,其实在iis6的2003server版本中有一个“通配符应用程序映射”:

添加了这个就等于把所有的请求都交给了.net,这样实现的防盗链,即使是迅雷或是别的什么下载工具照样是下不了的,下的文件名虽然是那个但是内容就完全不是了,嘿嘿。。。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网