Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • pbl_dk 150 posts 551 karma points
    Sep 13, 2017 @ 13:17
    pbl_dk
    0

    Hi there.

    Im building a iHttpHandler based on the tutorial here:

    https://www.steadygo.digital/blog/securing-media-files-umbraco-7-5-2/

    My problem is that Im adding a function which needs info from:

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    

    When I add that helper I get an error:

    System.ArgumentNullException: 'A value cannot be null.
    Parameternavn: umbracoContext'
    

    Does anyone know why theres no value i UmbracoContext? I have put the code in App_Code.

    The code is run from the webconfig in the media folder:

      <add name="PDFfilter" path="*.pdf" verb="*" type="HttpHandlerSandbox.App_Code.HelloWorldHandler" />
    
  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Sep 13, 2017 @ 13:24
    Damiaan
    102

    Hi

    The UmbracoContext is the current umbraco request. Because the request is not entered into umbraco yet, you can not use the UmbracoHelper.

    What you could do, but I would not suggest to walk this way.

    UmbracoContext.EnsureContext(  
                new HttpContextWrapper(HttpContext.Current),  
                ApplicationContext.Current,  
                true);  
    

    What do you need this for?

    Kind regards
    Damiaan

  • pbl_dk 150 posts 551 karma points
    Sep 13, 2017 @ 14:14
    pbl_dk
    0

    I need it to compare a list of existing media.Urls()'s to the incoming httphandler.

    So I take out a complete list of mediaurls from the umbraco context. When the httphandler is called, it should compare the current request to the list of media, to see if the media it is calling is protected. Everything else works, but the comparison.

    Why should'nt I use the code? Is there a risk?

    I actually don't know what to do if context doesnt work, then I might have to make a cronjob and a controller which fires lists into a DB entry every 5 minutes.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Sep 13, 2017 @ 14:19
    Damiaan
    1

    Yes, there is a risk.

    If you take the complete list of all media urls, your site might because massively slow! You should do as less lookups as possible. Also, most of the media files exists on disk. So you could check if the file exists on disk to continue with your handler.

    If you need to protect media, also consider a package like Media Protect.

    Kind regards

  • pbl_dk 150 posts 551 karma points
    Sep 13, 2017 @ 14:24
    pbl_dk
    0

    okay. I have to think about what to do from here.

    Thanks for your input, Damiaan, really appreciate it.

    :-) /Peter

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Sep 13, 2017 @ 14:27
    Damiaan
    0

    Good luck. You aren't tackling the easiest thing.

  • pbl_dk 150 posts 551 karma points
    Sep 15, 2017 @ 19:25
    pbl_dk
    0

    I ended up stuffing it all into the ApplicationEventHandler with ContentService_Published. Seems to work. Also installed Entity Framework to put all content into a separate table.

    Shouldnt be any problems with Umbraco.Context and ApplicationEventHandler as far as you know..?

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Sep 18, 2017 @ 10:31
    Ismail Mayat
    1

    Please read https://our.umbraco.org/documentation/Reference/Common-Pitfalls/ there is section on newing up UmbracoContext.

    Regards

    Ismail

  • pbl_dk 150 posts 551 karma points
    Sep 18, 2017 @ 18:31
    pbl_dk
    0

    Thanks Ismail. Thats interesting reading. Havent seen that, though I should. A new approach might be needed for this project.

  • pbl_dk 150 posts 551 karma points
    Sep 18, 2017 @ 18:45
    pbl_dk
    0

    Seems I was lucky this time. My script is accessing via this:

    "access each of these services from their own singleton constructs: UmbracoContext.Current and ApplicationContext.Current."

    Thanks for input both of you guys.

Please Sign in or register to post replies

Write your reply to:

Draft