Friday 16 October 2015

Sitecore Glass doesn't work with MediaRequestProtection

There's a new feature in Sitecore 7.5, called Media Request Protection.  This is where Sitecore adds a hash to the media URL, to stop it being tampered with.  You can manage it in the Sitecore.Media.RequestProtection.config file.  

In fact if you need to add to the parameters in the Media URL, perhaps for a custom image handler; you will need to ensure that your new parameters are registered in this configuration file, otherwise they won't come through to your handler.
 
We just went live with new Sitecore 8.0 site and noticed lots of errors in the log something like: 

948 20:44:25 ERROR MediaRequestProtection: An invalid/missing hash value was encountered. The expected hash value: E43FE65CB6A819B4976A19BEFF730F204C7386DB. Media URL: /~/media/Images/Heroes/card-bikes.ashx?h=279&w=576, Referring URL: 7084 20:44:25 ERROR MediaRequestProtection: An invalid/missing hash value was encountered. The expected hash value: 098D0163B651C8F5944A0183BB29012295FA8B54. Media URL: /~/media/Images/Heroes/product-card.ashx?h=279&w=279, Referring URL: 

The errors seemed to be so frequent that it was causing memory to spike, which was impacting the site.

Also (and more importantly) what this meant was that Sitecore wasn't resizing/caching any of the images before sending them to the client.  Sitecore's own image handlers were not working.

On investigation we realized that Sitecore Glass's Mvc Helper methods like @Editable and @RenderImage were not rendering the new Media Request security hash. Doing a little digging I found: https://github.com/mikeedwards83/Glass.Mapper/issues/93 The article mentions that it is a known issue with Sitecore Glass and is fixed in Sitecore Glass version 4.0.

The options we came up with were 
  • Turn it off
  • Upgrade Sitecore Glass
  • Write our own helper method or extend the Sitecore Glass ones
In the end we opted to upgrade.

2 comments:

  1. When i am trying to get the index details using the code var index = ContentSearchManager.GetIndex("sitecore_custom_master_index");
    I am facing error like could not find the node content/configuration.
    basically we are using solr index

    ReplyDelete
  2. Hi Anath, it sounds like missing configuration. Here's a good article on how to set up Sitecore for use with Solr - its in 3 parts: http://sitecoreblog.patrickperrone.com/2015/02/sitecore-8-solr-part-13-install-tomcat.html - pay attention to the sections that deal with which files need to be enabled and disabled.

    Without seeing what you have set up its difficult to say what the issue is.

    Hope this helps,

    James.

    ReplyDelete