友情链接:.NET项目开发者博客

图片防盗链

    void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.RawUrl.Contains("images/"))
        {
            if (Request.UrlReferrer == null || !IsSameDomain(Request.UrlReferrer, Request.Url))
            {
                ////
                Response.ContentType = "image/jpeg";
                string path = Request.MapPath("~/daolian.jpg");
                Response.WriteFile(path);
                //结束请求
                Response.End();
            }
        }
    }
    //判断两个域名是否相等
    bool IsSameDomain(Uri u1,Uri u2)
    {
        return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
    }

posted @ 2013-03-26 23:53  千年老妖  Views(162)  Comments(0Edit  收藏  举报
友情链接:.NET项目开发者博客