Asp.net anti-leech

Source: Internet
Author: User

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;

Public class handler: ihttphandler {
   
   Public void processrequest (httpcontext context ){
       If (context. Request. urlreferrer. Host = "10.11.43.52 ")
       {
           Context. response. expires = 0;
           Context. response. Clear ();
           Context. response. contenttype = "image/jpg ";
           Context. response. writefile (context. Request. physicalpath );
           Context. response. End ();
       }
       Else
       {
           Context. response. expires = 0;
           Context. response. Clear ();
           Context. response. contenttype = "image/jpg ";
           Context. response. writefile (context. Request. physicalapplicationpath + "error.jpg ");
           Context. response. End ();
       }
   }
 
   Public bool isreusable {
       Get {
           Return true;
       }
   }

}

1. The general processing program file (which can be directly compiled under vs2008). If it is vs2005, it must be written to app_code and then compiled.

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

/// <Summary>
/// Handler Summary
/// </Summary>

 

Public class handler: ihttphandler {
   Public handler ()
   {
       //
       // Todo: add the constructor logic here
       //
   }
   
   Public void processrequest (httpcontext context ){
       If (context. Request. urlreferrer. Host = "10.11.43.52 ")
       {
           Context. response. expires = 0;
           Context. response. Clear ();
           Context. response. contenttype = "image/jpg ";
           Context. response. writefile (context. Request. physicalpath );
           Context. response. End ();
       }
       Else
       {
           Context. response. expires = 0;
           Context. response. Clear ();
           Context. response. contenttype = "image/jpg ";
           Context. response. writefile (context. Request. physicalapplicationpath + "error.jpg ");
           Context. response. End ();
       }
   }
 
   Public bool isreusable {
       Get {
           Return true;
       }
   }

}

2. Configure the Web. config file as follows: process and intercept handler content

<? XML version = "1.0"?>
<! --
   Note: In addition to manually editing this file, you can also use
   Web management tools to configure application settings. You can use
    "Website"-> "ASP. NET configuration" option.
   The complete list of settings and comments is displayed in
   In machine. config. Comments, this file is usually located in
   \ Windows \ Microsoft. NET \ framework \ v2.x \ config
-->
<Configuration>
 <Appsettings/>
 <Connectionstrings/>
 <System. Web>
  <! --
           Set compilation DEBUG = "true" to insert the debugging symbol
           Compiled pages. However, this
           Performance is affected, so this value is only available during development.
           Set to true.
       -->
  <Compilation DEBUG = "true"/>
  <! --
           In the <authentication> section, you can configure
           Security Authentication mode,
           To identify the user.
       -->
  <Authentication mode = "Windows"/>
  <! --
           If an unprocessed error occurs during request execution,
           You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
           This section allows developers to configure
           HTML error page to be displayed
           To replace the error stack trace.

       <Customerrors mode = "remoteonly" defaultredirect = "genericerrorpage.htm">
           <Error statuscode = "403" Redirect = "noaccess.htm"/>
           <Error statuscode = "404" Redirect = "filenotfound.htm"/>
       </Customerrors>
  
       -->
  <Httphandlers>
   <Add verb = "*" Path = "*. jpg" type = "handler"/> 
  </Httphandlers>
 </System. Web>
</Configuration>

3rd, configure iisand filter the iiss' asp.netprocessing content, and specify the same dll as aspx for the .jpg file.

Page code:

<% @ Page Language = "C #"Codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
   <Title> No title page </title>
</Head>
<Body>
   <Form ID = "form1" runat = "server">
   <Div>
        & nbsp; </div>
   </Form>
</Body>
</Html>

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.