asp.net - IIS禁止直接访问图像

标签 asp.net iis-7

我在 IIS 中创建了一个网站,根网络共享有一些子文件夹,用于存储页面正在使用的图像、css、js 文件。但是,如果用户知道图像名称( http://hello.com/images/abc.jpg ),他们就可以访问图像。

有什么方法可以禁止直接访问资源吗?请注意,我刚刚开始学习 ASP.NET,所以如果答案能够具有一定的描述性,那就太好了。

我已经了解了 URL 重写方法,但就是无法让它工作。

编辑:我将此 web.config 放在我的图像文件夹中,现在它执行相反的操作,阻止页面上的图像并直接允许它们。 如有任何帮助,我们将不胜感激。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <identity impersonate="true" />
    </system.web>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="RequestBlockingRule1" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url=".*\.(gif|jpg|png)$" /> 
            <conditions>
                        <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
                        <add input="{HTTP_REFERER}" pattern=" http://iolab023/.*" negate="true" /> 
            </conditions> 
                    <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

最佳答案

如果您想阻止直接访问上下文(从客户端/浏览器),您可以使用配置部分来阻止它。在站点根目录的 web.config 中,您可以使用此配置来禁止访问“images”子目录。如果您查看 applicationhost.config,您会发现此部分已配置为防止客户端直接访问“bin”文件夹。您只需将“图像”添加到该列表中,无论是在 applicationhost.config 中还是在 web.config 中,如下所示。

(如果您在 applicationhost.config 中根本看不到任何配置,这意味着您需要使用“添加/删除程序”或 Web 平台安装程序在 IIS 中安装 requestFiltering 功能)。

<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <hiddenSegments applyToWebDAV="true">
                    <add segment="images" />
                </hiddenSegments>
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

关于asp.net - IIS禁止直接访问图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057441/

相关文章:

jquery - 在 ASP.NET Web Forms 4.5 中捆绑 JQuery

asp.net - 在中继器中设置下拉列表的 Selectedindex/selectedvalue 不起作用

asp.net - 如何在 mvc4 中调用另一个 Controller 的 View

文件复制访问被拒绝问题的 IIS 7 和 503 错误

c# - http错误503服务不可用。应用程序池在访问网站时停止

c# - IIS 应用程序上的 ProtectedData.Unprotect - 在 IISRESET 后无法工作

asp.net - 将 ASP.NET MVC 应用程序部署到 IIS 的好方法是什么?

jquery - LinkedIn JavaScript API 无法在 HTTPS 上运行

c# - ASP.Net 请求生命周期 - Application_BeginRequest

c# - 高性能服务器 - 我应该使用什么?