asp.net网站 图片防盗链

新建一个Global.asax 程序 
 向里面添加以下代码
 void Application_BeginRequest(object sender, EventArgs e)
    {
        //图片防盗链
        if (Request.RawUrl.Contains("images/"))
        {
            if (Request.UrlReferrer == null || !IsSameDomain(Request.Url, Request.UrlReferrer))
            {
                Response.ContentType = "images/jpeg";
                string path = Server.MapPath("~/daolian.jpg");
                Response.WriteFile(path);
            }
        }
           
    }
 //判断两次url是否一致
    bool IsSameDomain(Uri uri1, Uri uri2)
    {
        return Uri.Compare(uri1, uri2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
    }

posted @ 2011-11-28 09:59  bjlhx15  阅读(258)  评论(2编辑  收藏  举报
Copyright ©2011~2020 JD-李宏旭