Asp. NET implementation of image anti-theft chain

Source: Internet
Author: User

Use Httphandle to achieve, special processing of requests for picture files
The first step: Create a class, inherit from IHttpHandler, the code is as follows

C # code

usingSystem;usingsystem.web;namespacecustomhandler{ Public classjpghandler:ihttphandler{ Public voidProcessRequest (HttpContext context) {//get the file server-side physical pathstringFileName =context. Server.MapPath (context. Request.filepath);//if Urlreferrer is empty, a picture of the default forbidden hotlinking is displayedif(Context. Request.UrlReferrer.Host = =NULL) {context. Response.ContentType="Image/jpeg"; context. Response.WriteFile ("/error.jpg");}Else{//If the urlreferrer does not contain its own site host domain name, a default forbidden hotlinking picture is displayedif(Context. Request.UrlReferrer.Host.IndexOf ("yourdomain.com") >0) {context. Response.ContentType="Image/jpeg"; context. Response.WriteFile (FileName);}Else{context. Response.ContentType="Image/jpeg"; context. Response.WriteFile ("/error.jpg");}}}

public bool isreusable{

get{return true;}

}

}

}


Step two: Compile into a DLL
Csc/t:library CustomHandler.cs
Step three: Add the compiled DLL reference to the Bin folder of the current site
Fourth Step: Register this handler in Web. config

C # code

 <  system.web  >  <  httphandlers  >  <  add  path  = "*.jpg,*.jpeg,*.gif,*.png,*.bmp"   verb  = "*"   type  = "Customhandler.jpghandler,customhandler"  />  </ httphandlers  >  </ system.web  >  

Verb refers to the way the file is requested, which can be a post or get, with * representing all access methods. Customhandler.jpghandler represents the namespace and class name, Customhandler represents the assembly name

Asp. NET implementation picture anti-theft chain (GO)

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.