2017-01-23 55 views

回答

1

有很多方法,你怎麼做「熱鏈接保護」。其中之一是使用重寫規則。它會顯示no_hotlinking_allowed.jpg圖像,如果有人會嘗試你的形象在其他網站鏈接:

<rewrite> 
     <rules> 
      <rule name="Hotlinking protection"> 
       <match url=".*\.(gif|jpg|png)$" /> 
       <conditions> 
        <add input="{HTTP_REFERER}" pattern="^$" negate="true" /> 
        <add input="{HTTP_REFERER}" pattern="^http://(.*\.)?domain\.com/.*$" negate="true" /> 
       </conditions> 
       <action type="Rewrite" url="/images/no_hotlinking_allowed.jpg" /> 
      </rule> 
     </rules> 
    </rewrite> 

它是通用的方法,並且它不涉及imageresizer

在情況下,如果u想避免以無水印quesrystring圖像,此規則將適合你:

<rule name="Autoadd watermark"> 
       <match url=".*\.(gif|jpg|png)$" /> 
       <conditions> 
        <add input="{QUERY_STRING}" pattern=".*watermark.*" negate="true" /> 

       </conditions> 
       <action type="Rewrite" url="{PATH_INFO}?watermark=watermark" /> 
      </rule> 
+0

謝謝。這是安全的方法,不是嗎? – Yargicx

+0

它正在爲盜鏈保護工作(所以,沒有人可以將您的圖片放置到其他網站)。但用戶仍然可以拍攝原件。 –

+0

編輯我的答案,提供了兩種解決方案 –

0

您可以添加自定義的授權規則,以ImageResizer所以它只會有水印提供圖片:

ImageResizer.Configuration.Config.Current.Pipeline.AuthorizeImage += 
    delegate(IHttpModule sender, HttpContext context, IUrlAuthorizationEventArgs e) 
    { 
     // You can also check that you support specific watermark parameter value. 
     if (string.IsNullOrEmpty(context.Request.QueryString["watermark"])) 
     { 
      e.AllowAccess = false; 
     } 
    }; 

欲瞭解更多信息,請檢查ImageResizer events documentation