.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

  

你可能感兴趣的:(.net)