使用 Image Resizer ASP.NET MVC 保护图像

Protect Images With Image Resizer ASP.NET MVC

我为 ASP.NET 使用 imageresizer。当我拿到照片时,我会放上我们的品牌。但是我怎样才能保护原始图片。 例如:

<img src="http://www.domain.com/mypic.jpg?width=400&watermark=mywatermark" />

但是有人可以从<a href="http://www.domain.com/mypic.jpg" rel="nofollow noreferrer">http://www.domain.com/mypic.jpg</a>拍到我的原图,我该如何保护它?

谢谢。

方法有很多,你怎么做"hotlink protection"。其中之一是使用重写规则。如果有人试图 link 您在另一个网站中的图片,它将显示 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无关

以防万一,如果您想保护对没有水印查询字符串的图像的访问,此规则将适合您:

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

您可以向 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