【问题标题】:Block direct access to url and allowing access by system only阻止直接访问 url 并仅允许系统访问
【发布时间】:2011-12-09 10:32:40
【问题描述】:

我想阻止除请求图像的 iis usr 之外的所有内容访问 url。

所以我有: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();

【问题讨论】:

  • 您是在服务器端请求文件还是访问您页面的客户端浏览器请求文件?
  • 图像是在服务器端请求的,但从查询字符串中获取输入。这有帮助吗?

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


【解决方案1】:

如果请求来自localhost,您可以签入您的myfile.ashx

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;
}

【讨论】:

  • 如果我们的服务器负载均衡,这会是个问题吗?
  • 我要做 ["SERVER_NAMES"] 这应该解决的情况会让你知道这是否有效。
  • 如果每台服务器只访问自己(而不是退出负载平衡器并返回),那么它们的负载平衡将无关紧要。
【解决方案2】:

IIS7 内置了请求过滤功能,如果您使用的是该版本或更高版本,它会有所帮助

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

【讨论】:

  • 感谢抱歉,我们目前仍处于使用 iis 6 的冰河时代。不过感谢您的提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2011-08-11
  • 2016-10-24
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多