阻止对url的直接访问,并允许仅通过系统进行访问

时间:2011-12-09 10:32:41

标签: c# asp.net security iis-6

我想阻止从请求图片的iis usr以外的所有内容访问网址。

所以我有:www.myurl.com/somedirectory/myfile.ashx

我只希望来自代码的请求能够访问此文件而不是用户/机器人/非客户端,他们可以通过转到URL来手动访问该文件。

需要考虑服务器负载均衡我不确定这是否会导致问题。

我将如何做到这一点。

请求的服务器端:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
httpRequest.Method = "GET";
httpRequest.UserAgent = "MobileQ.NET";
httpRequest.ContentType = "image/png";
response = httpRequest.GetResponse().GetResponseStream();

2 个答案:

答案 0 :(得分:1)

IIS7内置了请求过滤功能,如果您正在使用该版本,可能会有所帮助

http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering

答案 1 :(得分:1)

如果请求来自myfile.ashx

,您可以签入localhost
string client = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if(client == "localhost" || client == "127.0.0.1" || client == "::1")
    //DoWork
else
{
    HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;
    return;
}
相关问题