.net 防盗链

Global.asax 文件中

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //判断当前请求是否是访问 images/文件夹
            if (Request.RawUrl.ToLower().Contains("/images/"))
            {
                Uri referrer = Request.UrlReferrer;
                Uri url = Request.Url;
                if (!CompareURL(url, referrer))
                {
                    //输出盗链图片
                    string path = Request.MapPath("~/images/daolian.gif");
                    Response.WriteFile(path);
                    //结束请求
                    Response.End();
                }
            }
        }

        bool CompareURL(Uri u1, Uri u2)
        {
            return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0;
        }

转:http://blog.csdn.net/zhangquanok/article/details/10072293

  

posted @ 2013-12-04 11:08  xyzhuzhou  阅读(213)  评论(0编辑  收藏  举报