asp.net picture file anti-theft chain (respect for labor achievements) and BeginRequest event Learning _ Practical skills

Source: Internet
Author: User
About the picture hotlinking this problem, after all, is the success of their own work, many people do not want others to steal it so easily. This feature is on many forums, probably because hotlinking has too much to act on.

Anti-hotlinking program is actually very simple, familiar with the ASP.net application lifecycle words can easily write a, the use of HttpModule in the BeginRequest event interception request on the OK, the rest of the work is filtered, and then filtered!

If you are not familiar with HttpModule, you can go to the MSDN lookup, Introduction very detailed, Address: Ms-help://ms. Vscc.v80/ms. Msdn.v80/ms. Visualstudio.v80.chs/dv_aspnetcon/html/f1d2910f-61d0-4541-8af8-c3c108ca351f.htm, there's no more nonsense here.
Copy Code code as follows:

private void Application_BeginRequest (Object source, EventArgs E)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = Application. context;
bool Issafe = true; is legal link
String uri = context. Request.Url.AbsolutePath.ToLower ();
if (URI. LastIndexOf (".") ) > 0 && context. Request.urlreferrer!= null)
{
String exp = URI. Substring (Uri.lastindexof (".") ));
This is to determine if the file suffix name is within the excluded file type list
BOOL Ishas = ClassLibrary.RData.RString.StrIsIncUseSC (exp, config. Imgsafetype.split (' | ') ));
if (Ishas)
{
String domainoutter = context. Request.UrlReferrer.Authority.ToLower ();//include domain name and port
ArrayList Arry = Common.Cache.GetDomainValid ()//To take a system-defined legal domain name binding list
Issafe = Arry. Contains (domainoutter);//Determine whether the current requested domain name is within the legal list
}
}
The following is illegal when the output, if there is a default alternative to the picture output, if not on the generation of a, format. Gif
if (! Issafe)
{
Bitmap img = null;
Graphics g = null;
MemoryStream ms = NULL;

Try
{
String picpath = ClassLibrary.RPath.GetFullDirectory ("Images/unlawful.gif");
if (file.exists (Picpath))
{
img = new Bitmap (Picpath, false);
}
Else
{
img = new Bitmap (* *, * *);
g = Graphics.fromimage (IMG);
G.clear (Color.White);
Font f = new font ("Arial, Blackbody, Arial", 9,fontstyle.bold);
SolidBrush s = new SolidBrush (color.red);
g.DrawString (Resources.Message.LawlessLink, F, S, 1, 20);
Img. Save (Picpath, imageformat.gif);
}
ms = new MemoryStream ();
Img. Save (MS, Imageformat.gif);
Context. Response.clearcontent ();
Context. Response.ContentType = "Image/gif";
Context. Response.BinaryWrite (Ms.toarray ());
Context. Response.End ();
}
Catch
{ }
Finally
{
if (g!= null)
G.dispose ();
Img. Dispose ();
}
}
}

All benefits will be harmful, the biggest drawback is to increase the system overhead, the client every request to filter again, the performance of nature to discount. Do not know which friend has a better way, or the optimization method, together to explore

the function of implementing file-putting hotlinking is renewed
First add a global file global.asax
In Application_BeginRequest we can determine whether the Urlreferre in the HTTP header is sourced from this site.
Copy Code code as follows:

if (HttpContext.Current.Request.UrlReferrer!= null)
{
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith ("JPG", stringcomparison.ordinalignorecase) && HttpContext.Current.Request.UrlReferrer.Host!= "localhost")
{
HttpContext.Current.Response.WriteFile (HttpContext.Current.Server.MapPath ("~/jzdl.jpg"));
HttpContext.Current.Response.End ();
}
}

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.