ASP.net中怎么利用UrlRewrite实现防盗链功能

发布时间:2021-08-09 17:45:04 作者:Leah
来源:亿速云 阅读:132

ASP.net中怎么利用UrlRewrite实现防盗链功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

配置文件: 

<?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的一样就成)

看完上述内容,你们掌握ASP.net中怎么利用UrlRewrite实现防盗链功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. urlrewrite
  2. ASP.NET 实现防盗链

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net urlrewrite

上一篇:asp.net中怎么利用Zxing库输出条形码

下一篇:asp.net 中如何实现文件上传功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》