asp.net下结合HttpHandler实现图片防盗链
程序员文章站
2024-03-08 13:19:34
复制代码 代码如下: #region ihttphandler 成员 bool ihttphandler.isreusable { get { return true; }...
复制代码 代码如下:
#region ihttphandler 成员
bool ihttphandler.isreusable
{
get { return true; }
}
void ihttphandler.processrequest(httpcontext context)
{
string filename = context.server.mappath(context.request.filepath);
if (context.request.urlreferrer.host == null)
{
context.response.contenttype = "image/jpeg";
context.response.writefile("/no.jpg");
}
else
{
if (context.request.urlreferrer.host.indexof("mydomain.com") > 0)
{
context.response.contenttype = "image/jpeg";
context.response.writefile(filename);
}
else
{
context.response.contenttype = "image/jpeg";
context.response.writefile("no/jpg");
}
}
}
#endregion
<httphandlers>
<add verb="*" path="*.jpg" type="jpghandler, mydll" />
</httphandlers>