Using System;
Using System. Web;
/// <Summary>
/// Abstract description of ImageProtect
/// </Summary>
Public class ImageProtect: IHttpHandler
{
Public ImageProtect ()
{
//
// TODO: add the constructor logic here
//
}
Public void ProcessRequest (HttpContext context)
{
// Determine whether the image is a local reference. If yes, the correct image is returned to the client,
// Assume that the website address is http: // localhost: 6999. You can modify the address as needed during demonstration.
If (context. Request. UrlReferrer. Host = "localhost" & context. Request. UrlReferrer. Port = 6999)
{
// Set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. Response. Expires = 0;
// Clear the output cache opened by the server for this session
Context. Response. Clear ();
// Obtain the file type
Context. Response. ContentType = "image/jpeg ";
// Write the request file to the server's output cache for this session
Context. Response. WriteFile (context. Request. PhysicalPath );
// Transfer the information in the output cache opened by the server for this session to the client
Context. Response. End ();
}
Else // if it is not a local reference, it is a leeching reference and an error image is returned to the client.
{
// Set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. Response. Expires = 0;
// Clear the output cache opened by the server for this session
Context. Response. Clear ();
// Obtain the file type
Context. Response. ContentType = "image/jpeg ";
// Write the image file with special report errors to the output cache opened for this session on the server side
Context. Response. WriteFile ("~ /Images/error.jpg ");
// Transfer the information in the output cache opened by the server for this session to the client
Context. Response. End ();
}
}
Public bool IsReusable
{
Get
{
Return false;
}
}
}
/*
* In the configuration file
* <HttpHandlers>
* <Add verb = "*" path = "*. jpg" type = "ImageProtect"/>
</HttpHandlers>
*/
From happy pig's column