Copied to clipboard

Flag this post as spam?

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


  • Elitenet 77 posts 320 karma points
    Sep 13, 2017 @ 13:17
    Elitenet
    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 380 posts 1045 karma points 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

  • Elitenet 77 posts 320 karma points
    Sep 13, 2017 @ 14:14
    Elitenet
    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 380 posts 1045 karma points 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

  • Elitenet 77 posts 320 karma points
    Sep 13, 2017 @ 14:24
    Elitenet
    0

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

    Thanks for your input, Damiaan, really appreciate it.

    :-) /Peter

  • Damiaan 380 posts 1045 karma points c-trib
    Sep 13, 2017 @ 14:27
    Damiaan
    0

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

  • Elitenet 77 posts 320 karma points
    Sep 15, 2017 @ 19:25
    Elitenet
    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 3991 posts 8154 karma points admin mvp 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

  • Elitenet 77 posts 320 karma points
    Sep 18, 2017 @ 18:31
    Elitenet
    0

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

  • Elitenet 77 posts 320 karma points
    Sep 18, 2017 @ 18:45
    Elitenet
    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