隐藏

asp.net 实现防迅雷等下载工具盗链

发布:2020/1/2 17:36:21作者:管理员 来源:本站 浏览次数:916

利用IHttpHandler接口来监听对本网站的资源请求后缀名是否是我们要阻止的文件,如果是再判断是否有下载权限。没有就给它返回一个默认的无用的文件。
主要代码如下:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/octet-stream";
HttpRequest req = context.Request;
string filename = req.Url.AbsolutePath;
string userid = string.Empty;
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["userid"] != null)
{
userid = HttpContext.Current.Request.Cookies["userid"].Value;
}
if (userid == "1")
{
string uuu = context.Server.MapPath(filename);
context.Response.TransmitFile(uuu);
}
else
{
string u2 = context.Server.MapPath("default.rar");
context.Response.WriteFile(u2);
}
}

设计思想如下:
1. 利用IHttpHandler接口来监听对本网站的资源请求后缀名是否是我们要阻止的文件,如果是再判断是否有下载权限。没有就给它返回一个默认的无用的文件。