Skip to content

Instantly share code, notes, and snippets.

@peaeater
Created September 28, 2020 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peaeater/239aefa98fb539b1b000c1fbeb4650d1 to your computer and use it in GitHub Desktop.
Save peaeater/239aefa98fb539b1b000c1fbeb4650d1 to your computer and use it in GitHub Desktop.
Force ImageResizer to authorize images larger than thumbnail based on user role in ASP.NET MVC
protected void Application_Start()
{
// various setup bits here...
Config.Current.Pipeline.AuthorizeImage +=
delegate (IHttpModule sender, HttpContext context, IUrlAuthorizationEventArgs e)
{
// only restrict ~/media
if (!e.VirtualPath.StartsWith(VirtualPathUtility.ToAbsolute("~/media")))
{
return;
}
var estimatedSize = ImageBuilder.Current.GetFinalSize(new System.Drawing.Size(1000, 1000),
new ResizeSettings(e.QueryString));
if (estimatedSize.Width <= 300 || estimatedSize.Height <= 300)
{
// it's 300px or less, let it go
return;
}
var roles = UserRoleHelper.RolesForUser(context.User.Identity);
e.AllowAccess = UserRoleHelper.RoleMayAccessFile(roles, e.VirtualPath, VirtualPathUtility.ToAbsolute("~/media/armstrong"));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment