您好,登錄后才能下訂單哦!
ASP.net中怎么利用UrlRewrite實(shí)現(xiàn)防盜鏈功能,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
配置文件:
<?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>
說(shuō)明:
CheckType:要求驗(yàn)證的類型(1:只驗(yàn)證合法的域名,2:只驗(yàn)證是否有cookies,3:同時(shí)驗(yàn)證域名與cookies)
CookiesName:要驗(yàn)證的cookies名稱,可為空。
UrlPattern:請(qǐng)求的URL格式。
UrlReplace:當(dāng)下載無(wú)效時(shí)轉(zhuǎn)向的URL格式。
AllowHost:允許的來(lái)源域名。
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中的配置:
可執(zhí)行文件填入:c:/windows/microsoft.net/framework/v2.0.50727/aspnet_isapi.dll(視實(shí)際情況變動(dòng),與.aspx的一樣就成)
看完上述內(nèi)容,你們掌握ASP.net中怎么利用UrlRewrite實(shí)現(xiàn)防盜鏈功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。