Skip to content

Instantly share code, notes, and snippets.

@ankittyagii
Last active February 26, 2020 11:19
Show Gist options
  • Save ankittyagii/ad4df60173e13b389ec2bf1e7a766979 to your computer and use it in GitHub Desktop.
Save ankittyagii/ad4df60173e13b389ec2bf1e7a766979 to your computer and use it in GitHub Desktop.
Prevent From Hotlinking in IIS/ASP.NET Framework/ASP.Net Core Application
<ItemGroup>
<None Update="hotlinkingURLRewrite.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<rewrite>
<rules>
<rule name="Prevent Image Hotlinking">
<match url=".*\.(jpg|doc)$"/>
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://demo.com/.*$" negate="true"/>
</conditions>
<action type="Rewrite" url="/images/block.jpg"/>
</rule>
</rules>
</rewrite>
{
public configure ()..{
using (var urlRewriteStreamReader = File.OpenText("UrlRewrite.xml"))
{
var options = new RewriteOptions().AddIISUrlRewrite(urlRewriteStreamReader);
app.UseRewriter(options);
}
}
}
<rewrite>
<rules>
<rule name="Prevent Image Hotlinking">
<match url=".*\.(jpg|doc)$"/>
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://demo.com/.*$" negate="true"/>
</conditions>
<action type="Rewrite" url="/images/block.jpg"/>
</rule>
</rules>
</rewrite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment