当前位置:编程学习 > asp >>

ASP.NET防盗链演示代码

C#代码如下:


<%@ WebHandler Language="C#" Class="防盗链" %> 
 
using System; 
using System.Web; 
 
public class 防盗链 : IHttpHandler { 
     
    public void ProcessRequest (HttpContext context) { 
        context.Response.ContentType = "image/JPEG"; 
        string fullpath = HttpContext.Current.Server.MapPath("~/image/22.jpg"); 
        using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath)) 
        { 
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 
            { 
                if (context.Request.UrlReferrer == null) 
                { 
                    g.Clear(System.Drawing.Color.White); 
                    g.DrawString("禁止提交图片", new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.RosyBrown, 0, 0); 
                } 
                else if(context .Request.UserHostAddress !="localhost"||context .Request .UserHostAddress !="127.0.0.1") 
                { 
                    g.Clear(System .Drawing .Color .White ); 
                    g.DrawString("禁止盗链",new System.Drawing.Font ("宋体",30),System .Drawing .Brushes .RosyBrown ,0,0); 
                } 
                     
            } 
            bitmap.Save(context .Response .OutputStream ,System .Drawing .Imaging .ImageFormat.Gif ); 
        } 
    } 
  
    public bool IsReusable { 
        get { 
            return false; 
        } 
    } 
 


摘自 编程爱好者
补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,