2014-09-26 84 views
2

我有一個在IIS中創建的網站,根網站共享有一些子文件夾,用於分發網頁正在使用的圖像,CSS,js文件。但是,如果用戶知道圖像名稱,則可以訪問圖像(http://hello.com/images/abc.jpg)。IIS禁止直接訪問圖像

有什麼辦法可以禁用資源的直接訪問嗎?請注意,我剛開始學習asp.net,所以如果答案可能有點描述性,那將會很棒。

我已經開始瞭解URL重寫方法,但是如何無法使其工作。

編輯:我把這個web.config放在我的images文件夾中,現在它做相反的事情,屏蔽頁面上的圖像並直接允許它們。 任何幫助表示讚賞。

<?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> 

回答

4

如果你想防止上下文直接訪問(從客戶端/瀏覽器),你可以使用配置部分,以阻止它。在您網站根目錄的web.config中,您可以使用此配置禁用「images」subdir被訪問。如果你看一下你的applicationhost.config,你會看到這個部分已經配置爲防止客戶端直接訪問「bin」文件夾。您只需要將「images」添加到該列表中,無論是在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>