Try our conversational search powered by Generative AI!

Saving media with AccessLevel.NoAccess still throws exception: Access was denied to content 6116_10194. The required access level was "Edit, Publish".


I have code to create media assets (in this example image files) which was previously working but now gives the access denied exception.  The same accesslevel mechanism seems to work fine for pages and blocks. NOTE: this is a slightly hacked up version showing the mechanism used, but mechanically is the same as code that previously worked.  So I'm wondering if anyone has ideas on where else to be looking in terms of what changed?  One question I have is why does it say it needs Edit access when we are  creating the asset (or does the GetDefault actually do the create?)  Could it be related to needing edit access to the parent folder into which the asset is going?

I have an update, after poking around I found that contrary to the exception itself, the ImageFile is in fact being created in the proper folder.  So the question is, why when it appears to be actually performing the Save am I getting this Exception?

public ContentReference SaveImage(ContentReference parentFolder, string data, string fileName, string fileExtension)
    var mediaType = _mediaDataResolver.GetFirstMatching(fileExtension);
    var contentType = _contentTypeRepository.Load(mediaType);

    if (mediaType != typeof(ImageFile))
        return null;

    var imageFile = _contentRepository.GetDefault(parentFolder, contentType.ID);

    imageFile.Name = fileName;

    var imageBlob = _blobFactory.CreateBlob(imageFile.BinaryDataContainer, fileExtension);
    using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data)))
    imageFile.BinaryData = imageBlob;

    return _contentRepository.Save(imageFile, SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
Edited, Jun 19, 2018 17:12

Can you see from the call stack in the exception where it is thrown from? My guess is that there are some event handler that listens to e.g. IContentEvent.PublishedContent and in the event handler re-saves that content instance (but without AccessLevel.NoAccess)

Jun 20, 2018 9:24
<p>Sure enough, someone had added an event handler catching the publish of MediaData and modifying them, and that code doesn't use the AccessLevel.NoAccess parameter on its save.&nbsp; Thanks for your help.</p>
Jun 20, 2018 14:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.