Base solution for your next web application
Open Closed

Download file with url query string. Auth error #9571


User avatar
0
mmorales created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version? v8.7
  • What is your product type (Angular or MVC)? angular
  • What is product framework type (.net framework or .net core)? netcore

If issue related with ABP Framework

  • What is ABP Framework version?

If issue is about UI

  • Which theme are you using?

I'm try to request a download genearting a url query string. i found angular code within the project (file-download helper)The second method is the one I created, when calling this method I get an auth error on the core side.( INFO 2020-08-27 16:54:58,612 [5 ] Microsoft.AspNetCore.Hosting.Diagnostics - Request starting HTTP/2 GET https://localhost:44301/FileManager/GetFile?fileName=lvweb01.zip&fileToken=6c3041bd-a1f6-1634-121d-f0252cb4cb3d INFO 2020-08-27 16:54:58,632 [5 ] uthorization.DefaultAuthorizationService - Authorization failed). Please advise **Angular ** export class FileDownloadService { downloadTempFile(file: FileDto) { const url = AppConsts.remoteServiceBaseUrl + '/File/DownloadTempFile?fileType=' + file.fileType + '&fileToken=' + file.fileToken + '&fileName=' + file.fileName; location.href = url; //TODO: This causes reloading of same page in Firefox } downloadFiles(fileName: string, fileToken: string) { const url = AppConsts.remoteServiceBaseUrl + '/FileManager/GetFile?fileName=' + fileName + '&fileToken=' + fileToken; location.href = url; //TODO: This causes reloading of same page in Firefox } } Core public async Task<FileStreamResult> GetFile(string fileName, string fileToken) { Stream stream = null; string filePath = Path.Combine(_env.WebRootPath, $"Common{Path.DirectorySeparatorChar}", $"Files{Path.DirectorySeparatorChar}", fileToken); stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

    var fileStreaResult = new FileStreamResult(stream, "application/pdf");
    fileStreaResult.FileDownloadName = fileName;
    return fileStreaResult;
}

5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If the download entpoint is an authorized endpoint, you have to include "enc_auth_token" query string parameter. You can get value of enc_auth_token as we do here https://github.com/aspnetzero/aspnet-zero-core/blob/dev/angular/src/app/shared/layout/chat/chat-message.component.ts#L32

  • User Avatar
    0
    mmorales created

    Thanks for your reply, after adding the enc_auth_token I get this error on the netcore end

    INFO 2020-08-31 12:02:08,653 [37 ] uthorization.DefaultAuthorizationService - Authorization failed. INFO 2020-08-31 12:02:08,686 [37 ] tion.Cookies.CookieAuthenticationHandler - AuthenticationScheme: Identity.Application was challenged.

  • User Avatar
    0
    mmorales created

    Thanks a lot for your help I was finally able to get downloads to work for large files. I eded up using window.location.href=url; instead of location.href= url;

  • User Avatar
    0
    ismcagdas created
    Support Team

    @mmorales thank you for your feedback.

  • User Avatar
    0
    insolutions created

    @ismcagdas What is the difference between including "enc_auth_token" query string parameter and Authorization header in http get request? Can I use either of them or are there some specific use cases fo each one of them?