Yargicx
Yargicx

Reputation: 1734

Protect Images With Image Resizer ASP.NET MVC

I use imageresizer for ASP.NET. I put our brand when i get pictures. But how can i protect original picture. For example:

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

But a person can take my original picture from http://www.domain.com/mypic.jpg How can i protect it?

Thank you.

Upvotes: 0

Views: 308

Answers (2)

Lionia Vasilev
Lionia Vasilev

Reputation: 12748

You can add custom authorization rule to ImageResizer so it will only serve images with watermarks:

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

For more information check ImageResizer events documentation.

Upvotes: 0

Victor Leontyev
Victor Leontyev

Reputation: 8736

There are many ways, how you can do "hotlink protection". One of this is using rewrite rule. It will show no_hotlinking_allowed.jpg image if someone will try to link your image in another website:

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

It is universal way, and it's not related to imageresizer

In case, if u want to protect access to images without watermark quesrystring, this rule will suit for you:

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

Upvotes: 2

Related Questions