Protecting Media in Umbraco 10+
c
What packages or solutions are you using for protecting media items in Umbraco 10+?
h
I have my own middleware class
f
c
@huwred are you able to share your middleware class at all please?
h
I'll dig it out later, out having lunch
c
Cheers mate
h
Code attached, I have roles assigned to media items so access is based on those, but you should get the jist of it
registered in startup
Copy code
csharp
            app.UseAuthentication();
            // Create branch to the ProtectedMediaHandler. 
            // All requests ending containing media will follow this branch.
            app.ProtectedMediaHandler();

            app.UseRouting();
Copy code
csharp
    public static IApplicationBuilder ProtectedMediaHandler(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ProtectedMediaHandlerMiddleware>();
    }
m
to late to reply on this @CodeSharePaul , but i do exactly the same as @huwred. but in our case it's slightly different as we use jwt tokens to validate members / groups. but the logic is preaty much the same.
c
Thanks @huwred and @mteodoro I'll see if I can get something working with this and provide back any improvements if I make them
@huwred @mteodoro I've got it set up in a basic site. Got it debugging but it only hits when i request a page and if i request an image url it doesn't hit the middleware
h
I'll test mine, what umbraco version are you using?
m
hum... do you have more then one Middleware?
c
latest 12.1.2
It's a clean umbraco site with clean starter kit installed and then this code added
m
i think you must attach the middleware before the ones from umbraco. let me check my startup class
c
Thanks
h
Needs to come before the umbraco stuff especially anything to do with routing
m
yes, it's in fact the first thing i do inside the Configure method @CodeSharePaul
c
Ok I will try that thank you.
Thanks, got it working now. I think the image was being cached by Image Sharp
c
Media Protect doesn't work on Linux though. Just so you know 😉
15 Views