Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning File in a site with Authentication
    primarykey
    data
    text
    <p>crosspost: <a href="https://orchard.codeplex.com/discussions/471384" rel="nofollow">https://orchard.codeplex.com/discussions/471384</a></p> <p>I'm using Orchard CMS 1.7 and we locked the entire site from anonymous users (admin -> users -> roles -> untick site front-end). Now, each page or file requires folks to be authenticated before getting access to them.</p> <p>Now, we're trying to provide the option of having some files available for download, depending on a field from a specific Content Type.</p> <p><strong>The ISSUE</strong></p> <p>When I download the file while I am authenticated, I get the it without issues. If I'm anonymous, I get the file (correct filename and type) but it has no contents and size (0 bytes). I'm pretty sure this is an auth issue since it all works well when I enable anonymous access to site front end.</p> <p>This is the actual code of returning the file (redirectLink is the filepath):</p> <pre><code> var cd = new System.Net.Mime.ContentDisposition { FileName = fileName, Inline = false }; Response.AppendHeader("Content-Disposition", cd.ToString()); return File(HttpUtility.UrlDecode(redirectLink), mimeType); </code></pre> <p>Here is my complete code (Controller Action):</p> <pre><code> [AlwaysAccessible] public ActionResult Download(int resourceId) { //set default unsecure value to false bool isUnsecured = false; var resourceItem = ContentManager.Get(resourceId); if (resourceItem == null || resourceItem.ContentType != "Resource") { // TODO: log that ID not found?? return new HttpStatusCodeResult(HttpStatusCode.NotFound); } var resourcePart = resourceItem.Parts.FirstOrDefault(p =&gt; p.PartDefinition.Name == resourceItem.ContentType); //retrieve unsecure boolean if (resourcePart != null) { var unsecuredField = resourcePart.Fields.FirstOrDefault(f =&gt; f.Name == "Unsecured"); if (unsecuredField != null) { isUnsecured = unsecuredField.Storage.Get&lt;bool&gt;(); } } //check if unsecured resource / allow anonymous downloads //see Orchard.Security.SecurityFilter - I'm not sure where this is actually used in Orchard though... if (!isUnsecured &amp;&amp; !Services.Authorizer.Authorize(StandardPermissions.AccessFrontEnd, T("Unauthenticated"))) { return new HttpUnauthorizedResult(); } if (resourcePart != null) { // TODO: Potential concurrency issues? var downloadCountPart = resourcePart.As&lt;DownloadCountPart&gt;(); if (downloadCountPart != null) { downloadCountPart.Total++; ContentManager.Publish(resourceItem); } // Do the redirection/serving of item! // Prioritize link field over resource field, according to the UI var linkedField = resourcePart.Fields.FirstOrDefault(f =&gt; f.Name == "LinkedFile"); if (linkedField != null) { var redirectLink = linkedField.Storage.Get&lt;string&gt;(); if (redirectLink != null) { return Redirect(redirectLink); } } var resourceField = resourcePart.Fields.First(f =&gt; f.Name == "ResourceFile"); if (resourceField != null) { var resourceMPF = (resourceField as MediaLibraryPickerField); if (resourceMPF != null &amp;&amp; resourceMPF.MediaParts != null &amp;&amp; resourceMPF.MediaParts.Count() &gt; 0) { var fileName = resourceMPF.MediaParts.First().FileName; var mimeType = resourceMPF.MediaParts.First().MimeType; var redirectLink = resourceMPF.MediaParts.First().MediaUrl; //to check: first? when are there multiple? if(!string.IsNullOrWhiteSpace(redirectLink)) { var cd = new System.Net.Mime.ContentDisposition { FileName = fileName, Inline = false }; Response.AppendHeader("Content-Disposition", cd.ToString()); return File(HttpUtility.UrlDecode(redirectLink), mimeType); } } } } return new HttpStatusCodeResult(HttpStatusCode.NotFound); } </code></pre> <p>Which leads me to these questions:</p> <ol> <li>Event with return File, it still takes into account the current user and whether that person is authenticated? </li> <li>Can I use impersonation to bypass this issue?</li> </ol> <p>Any piece of advise or information would be highly appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload