2

I have an internal API that would fetch and return a fileresult. However, this API does not have any concept of authentication/role/permission checks and cannot be modified to do so.

I would like to create an web API endpoint on an existing ASP.NET Core 2 Web API to do permission check, make a call to this internal API and return the fileresult back to a web client.

Would it be possible to get the wrapper API endpoint to just pass whatever it fetches as a file result without having to reconstruct the response (e.g., specify file name, content type, etc)? The files could be images, pdfs, document. I would prefer that this wrapper API only do permission check and make a call to the internal API endpoint using some sort of fileId and not need to know about the content length or type.

frostshoxx
  • 538
  • 1
  • 7
  • 24
  • 1
    Couldn't you use a reverse proxy like NGINX to do this? or a service gateway. – Blast_dan Feb 27 '18 at 16:48
  • Unfortunately, I do not have an access to the resources that would let me go with that path. – frostshoxx Feb 27 '18 at 16:50
  • 1
    Short answer: no. You can stream the HttpClient response body, but you'd still have to manually set the status code and response/content headers. – Chris Pratt Feb 27 '18 at 18:18
  • 1
    You might consider writing your own `IActionResult` implementation that does all this (i.e. `HttpClientResponseResult`). You can then also add an extension to `Controller` to utilize this custom implementation, so that it works just like the other built-ins (i.e. `return HttpClientResponse(response);`. – Chris Pratt Feb 27 '18 at 18:23
  • 1
    Is the second API publicly accessible? If so, how about returning a `302` that redirects the browser to the file's URL? – Marc LaFleur Feb 27 '18 at 19:01

1 Answers1

6

Per @chris-pratt's recommendation, turned out it wasn't that complicate to reconstruct the result as I originally anticipated. I ended up implementing this way in case someone needs to do something similar here.

... some validation logic outside the scope of the question...
using (HttpClient client = new HttpClient())
                {
                    var file = await client.GetAsync($"{someURL}/{id}");
                    return new FileContentResult(await file.Content.ReadAsByteArrayAsync(), file.Content.Headers.ContentType.MediaType);
                }
frostshoxx
  • 538
  • 1
  • 7
  • 24