Created
September 28, 2020 23:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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