ASP.NET图片防盗链(使用一般处理程序)

<img src="你的一般处理程序的地址"/>
 context.Response.ContentType = "image/jpeg";

            Uri referenceUrl = context.Request.UrlReferrer;
            Uri requestUrl = context.Request.Url;

            //判断是否是本网站的请求
            if (Uri.Compare(referenceUrl, requestUrl, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCulture) == 0)
            {
                context.Response.WriteFile("a.jpg");
            }
            else {
                context.Response.WriteFile("error.jpg");
            }

建一个一般处理程序,专门用于提供图片。该一般处理程序内,主要是通过比较请求的URL来判断是否是盗链行为!

posted @ 2017-10-10 22:46  水墨晨诗  阅读(189)  评论(2编辑  收藏  举报