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

来源:爱站网时间:2020-04-09编辑:网友分享
我们在网站开发的时候,都会遇到这样的情况,其实对方网站要链接到我们网站的文件,不是把这些链接放到自己的服务器上,今天爱站技术频道和大家分享ASP.net中实现基于UrlRewrite的防盗链功能。

我们在网站开发的时候,都会遇到这样的情况,其实对方网站要链接到我们网站的文件,不是把这些链接放到自己的服务器上,今天爱站技术频道和大家分享ASP.net中实现基于UrlRewrite的防盗链功能。

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

下载配置文件:

复制代码 代码如下:



1
username










说明:

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 {
IsAllowDomain = true;
}
else
{
for (int HostI = 0; 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 {
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中的配置:

 

 





IIS中的配置:

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

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

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

爱站技术频道小编觉得ASP.net中实现基于UrlRewrite的防盗链功能的实践非常方便,而且实用性很强,所以大家可以放心操作。

上一篇:asp.net获取HTML表单File中的路径的方法

下一篇:asp.net实现md5加密

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载