图片防止盗链

global.asax文件中写

 

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 @ 2019-05-09 16:19  咖啡无眠  阅读(251)  评论(0编辑  收藏  举报