c# 圖片防盜鏈

xuxubaby發表於2014-04-23

整合環境下,基於HttpHandler實現:

WebConfig 增加配置:

<httpHandlers>
   <add verb="*" path="*.jpg,*.jpeg,*.gif,*.png,*.bmp" type=" type="MvcApplication1.Handlers.TestHandler,MvcApplication1""/>
</httpHandlers>


c# 程式碼

namespace MvcApplication1.Handlers
{
    public class TestHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Uri uri = context.Request.UrlReferrer;

            if (uri == null)
            {
                context.Response.Write("來源地址不合法");
                context.Response.End();
            }

            string host = uri.Host.ToString().ToLower();
            bool result = host.IndexOf("test.com") > -1;
            if (result)
                context.Response.WriteFile(context.Request.PhysicalPath);
            else
                context.Response.Write("來源地址不合法");

        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}




相關文章