配置ImageResizer Web.config仅适用于某些目录

时间:2015-10-30 16:31:02

标签: c# c#-4.0 httphandler imageresizer

产品使用:来自http://imageresizing.net的ImageResizer.dll

我有一个客户网站,我使用此产品动态调整其在线购物车的图像大小。该产品效果很好!我最近添加了一个自定义HTTPHandler( MyCustomSecurityHandler )来控制对特定目录中需要保护的图像的访问。只要我不尝试使用ImageResizer识别的QueryString参数“width”或“height”访问图像,MyCustomSecurityHandler的代码就可以正常工作。如果没有使用QueryString参数,ImageResizer将忽略该请求,我的Custom HTTPHandler会接收请求并完美地处理安全性。

问题:如果用户输入具有ImageResizer.dll识别的属性的图像的直接路径,则会忽略我的自定义处理程序并显示图像。

所需目标:

1)无论提供的任何查询字符串参数如何,当路径匹配时,我的客户图像处理程序都会触发。 (如果我删除了ImageResizer.dll的web.config条目,则此方法有效)

2)让imageresizer.dll识别QueryString参数并仅在图像来自以下文件夹或子文件夹时进行处理:{SITEROOT} / images / products /

问题:是否有人有使用此imageresizer.dll产品的经验,您是否可以指导我如何配置它以达到我想要的目标?

我修改了以下配置文件,以显示我正在使用的相关条目。

{MY SECURE DIRECTORY}

我网站上实际路径的占位符值

WebProjectAssembly.HTTPHandlers.MyCustomSecurityHandler

我的自定义HTTPHandler类

我的Web.Config文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="resizer" type="ImageResizer.ResizerSection,ImageResizer" requirePermission="false" />
  </configSections>

  <resizer>
     <!-- Unless you (a) use Integrated mode, or (b) map all requests to ASP.NET, 
          you'll need to add .ashx to your image URLs: image.jpg.ashx?width=200&height=20 -->
     <pipeline fakeExtensions=".ashx" defaultCommands="autorotate.default=true"/>

     <plugins>
       <add name="DiskCache" />
       <!-- <add name="PrettyGifs" /> -->
       <!-- <add name="SimpleFilters" /> -->
       <!-- <add name="S3Reader" /> -->
     </plugins>  
   </resizer>
  
  <system.web>
    <httpModules>
       <!--This is for IIS7/8 Classic Mode and Cassini-->
      <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
    </httpModules>
    <httpHandlers>
      <add verb="*" path="/{MY SECURE DIRECTORY}/*.jpg" type="WebProjectAssembly.HTTPHandlers.MyCustomSecurityHandler" />
    </httpHandlers>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add verb="*" path="/{MY SECURE DIRECTORY}/*.jpg" name="MyCustomHandler" type="WebProjectAssembly.HTTPHandlers.MyCustomSecurityHandler" />
    </handlers>
    <modules>
      <!--This is for IIS7/8 Integrated mode--> 
      <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
    </modules>
  </system.webServer>
</configuration>

1 个答案:

答案 0 :(得分:0)

IIS/ASP.NET do not let you enable or disable modules based on directory, unless they are separate applications. However, given your task (control image access), you could just use ImageResizer v4's AuthorizeImage event with authorizeAllImages="true". That will fire on all requests, and let you meet your goals.