asp.net网站 图片防盗链
新建一个Global.asax 程序
向里面添加以下代码
void Application_BeginRequest(object sender, EventArgs e)
{
//图片防盗链
if (Request.RawUrl.Contains("images/"))
{
if (Request.UrlReferrer == null || !IsSameDomain(Request.Url, Request.UrlReferrer))
{
Response.ContentType = "images/jpeg";
string path = Server.MapPath("~/daolian.jpg");
Response.WriteFile(path);
}
}
}
//判断两次url是否一致
bool IsSameDomain(Uri uri1, Uri uri2)
{
return Uri.Compare(uri1, uri2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
}