標籤:style blog http color io os 使用 ar div
.net使用httpHandler添加圖片防盜鏈
1. 配置web.config:
<!--圖片添加浮水印的配置--> <httpHandlers> <add verb="*" path="*.jpg" type="LinkHandler" /> </httpHandlers> <!--圖片添加浮水印的配置結束-->
2. OutLinkHandler.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Drawing;using System.IO;/// <summary>///HttpHandler 實現防盜鏈效果/// </summary>public class LinkHandler:IHttpHandler{ public LinkHandler() { } //1. 設定不能允許重用 public bool IsReusable { get { return false; } } //2. 編寫最終處理常式 public void ProcessRequest(HttpContext context) { //context.Request.UrlReferrer.Host //主機名稱 //context.Request.Url.Port //連接埠號碼 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //context.Request.UrlReferrer.Authority <wbr>//伺服器端IP //根據Ip地址和連接埠號碼判斷 if (context.Request.UrlReferrer.Authority == "192.168.123.184" && context.Request.UrlReferrer.Port == context.Request.Url.Port) { context.Response.ContentType="image/jpeg"; context.Response.WriteFile(context.Request.PhysicalPath); } else { context.Response.ContentType="image/jpeg"; context.Response.WriteFile(context.Request.PhysicalApplicationPath+"images/1/LinkError.jpg"); } }}
.net使用httpHandler添加圖片防盜鏈