简体   繁体   English

拒绝IIS中的文件扩展名

[英]Denying file extensions in IIS

In IIS I noticed that I can deny access to any file extension on the server. 在IIS中,我注意到我可以拒绝访问服务器上的任何文件扩展名。 For example, I an deny access to .jpg images. 例如,我拒绝访问.jpg图像。 But then the browser won't be able to display .jpg images on my site. 但是,然后浏览器将无法在我的网站上显示.jpg图像。

I was wondering, does IIS allow us to deny access to file extensions - or filenames- that are being accessed directly (by typing the full url into the address bar), while still serving them to the browser if being requested by the document? 我想知道,IIS是否允许我们拒绝访问直接通过文件扩展名(或文件名)访问的文件扩展名(通过在地址栏中键入完整的url),同时仍按文档要求将其提供给浏览器?

You can't really prevent doing this; 真的无法阻止这样做; a determined person can download anything that a webpage can download. 确定的人可以下载网页可以下载的任何内容。 However, you can make it slightly more difficult for the less technically literate by checking the HTTP referrer in the request. 但是,通过检查请求中的HTTP引荐来源网址,您可以使技术水平较低的人稍有困难。 If it's the URL of one of your webpages, you can allow the request. 如果这是您其中一个网页的URL,则可以允许该请求。 Otherwise, you can deny it. 否则,您可以拒绝它。 This technique is used to prevent deep linking quite often. 此技术通常用于防止深度链接。

Here's a sample (untested) of how you can do this with a .NET HTTP module: 这是使用.NET HTTP模块的示例(未经试用):

public class ImageDenyingModule : IHttpModule
{
    public void Init(HttpApplication app)
    {
        app.BeginRequest += (s, e) => {
            var request = app.Context.Request;
            if (RequiresPageReferrer(request.Url) && !IsValidReferer(request.UrlReferrer)) {
                app.Context.Response.StatusCode = 404;
                app.Context.Response.End(); // Or something...
            }
        };
    }

    private bool RequiresPageReferrer(string url) {
    }

    private bool IsValidReferrer(string referrer) {
    }
}

You can do this with a custom http handler. 您可以使用自定义的http处理程序来执行此操作。

http://support.microsoft.com/kb/308001 http://support.microsoft.com/kb/308001

If you create a custom handler for jpg files then you can check the referer of the request and serve the image or cancel the request. 如果为jpg文件创建自定义处理程序,则可以检查请求的引荐来源并提供图片或取消请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM