Consider this scenario: I have a REST API built with Web API 2.x and authentication is managed by ASP.NET Identity. Users can upload attachments as part of their records, which are all stored on disk in the root folder, under a directory named Attachments
.
Now at some point users can generate PDFs from their records. The PDF includes links to attachment files, and when you click them the static file is happily served. For example: attachments/2018/01/somefile.jpeg
.
What I need to do, is serve these media files ONLY to authorized users. And basic authentication isn't enough, I can't just say serve media files for all logged-in users. I need to query the database, check user assignments and/or other security measures and decide if the file should be served or not.
I've been looking for a viable solution. How can I handle such routing through Web API and serve static content only to authorized users? It's worth mentioning that I don't use ASP.NET MVC in my project. It is just Web API 2.x with an Angular 1.6 app written in TypeScript.