Implement the UrlRewrite-based anti-leech function in ASP.net

Source: Internet
Author: User

This article describes how to implement UrlRewrite in ASP.net, which is just the simplest application.

In fact, using UrlRewrite and IIS settings, we can implement simple and effective anti-leech functions.

Assume that your website has a file: web.rar. You want to access it only by the source address of a specific domain name or a user who has logged on to it. In this case, you need to use the anti-leech feature. In the ASP era, we need to use third-party components to achieve this effect, but in ASP.net, we can directly use Context. rewritePath.

Download the configuration file:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<DownLoad>
<CheckType> 1 </CheckType>
<CookiesName> username </CookiesName>
<UrlPattern>
<! [CDATA [// (. + ?) /. Rar/B]>
</UrlPattern>
<UrlReplace>
<! [CDATA [Default. aspx? D1_1_1.rar]>
</UrlReplace>
<AllowHost>
<! [CDATA [127.0.0.1]>
</AllowHost>
</DownLoad>

Note:

CheckType: indicates the type to be verified. (1: only valid domain names are verified. 2: only cookies are verified. 3: Both domain names and cookies are verified)
CookiesName: the name of the cookie to be verified. It can be blank.
UrlPattern: the URL format of the request.
UrlReplace: the URL format to be switched when the download is invalid.
AllowHost: allowed source domain name.

Configuration in Global. aspx:
Copy codeThe Code is as follows:
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. receivettings ["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
{
}
}
}

Configuration in Web. Config:
Copy codeThe Code is as follows:
<Deleetask>
<Add key = "DownLoadConfig" value = "DownLoad. config"/>
</AppSettings>

Configuration in IIS:

Enter the executable file: c:/windows/microsoft.net/framework/v2.0.50727/aspnet_isapi.dll (as required, it is the same as. aspx)

Remember to remove the check box before checking whether the file exists.

You can add this to any file you want to anti-leech protection. In fact, there is a "wildcard application ing" in IIS6 2003Server ":

After this parameter is added, all requests are handed over. net, so the implementation of anti-Leech, even thunder or any other download tool is still not available, although the file name is that, but the content is completely not, hey...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.