Home > Web Front-end > JS Tutorial > asp.net HttpHandler implements image hotlink prevention_javascript skills

asp.net HttpHandler implements image hotlink prevention_javascript skills

WBOY
Release: 2016-05-16 18:42:12
Original
1089 people have browsed it

Step.1: Create the file CustomHandler.cs with the following code:

Copy the code The code is as follows:

using System;
using System.Web;

namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// Get the file server side Physical path
string FileName = context.Server.MapPath(context.Request.FilePath);
// If UrlReferrer is empty, a default hotlink-prohibited image will be displayed
if (context.Request .UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// If the UrlReferrer does not contain the domain name of your own site's host, a default picture that prohibits hotlinking will be displayed
if (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.2 Compile this file
Copy the code The code is as follows :

csc /t:library /r:System.Web.dll CustomHandler.cs

Step.3 Copy the compiled CustomHandler.dll to the Bin of the site directory.
Step.4 Register this Handler in Web.Config.
Copy code The code is as follows:






OK, you can test it yourself according to the steps, so I won’t go into details here.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template