Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I serve static files only to authorized users?

I have a collection of Excel spreadsheets that I'd like to serve in my ASP.NET 5 webapp only to authorized users.

  1. Where should I store the files? I assume in wwwroot (e.g., wwwroot/files).
  2. If in wwwroot, how do I allow access only to authorized users? (I'd like to serve them up as a [Authorize] FileResult from the controller, but this still leaves the files open to direct access through a URL I believe.)
  3. How do I reference a location in wwwroot through my FileResult action in the controller?

Thanks much!

like image 260
Gabe Avatar asked Apr 21 '16 16:04

Gabe


People also ask

How do you secure a static file?

We can protect static files with authorization on the ASP.NET Core web application by using the OnPrepareResponse property of the options argument for "Static Files" middleware. Don't forget that place the calling UseAuthentication() at before of the calling UseStaticFiles(...) .

How do you serve a static file?

To serve static files for Go 1.12+ in the standard environment, you define the handlers in your app. yaml file using either the static_dir or static_files elements. The content in the static files or static directories are unaffected by the scaling settings in your app.


1 Answers

Yes, they should go in wwwroot. Currently there is no built-in way to secure wwwroot directories. But creating a middleware module to accomplish it is pretty straightforward. There is an easy to follow tutorial here.

If you're not familiar with developing middleware, I posted a GitHub project that shows how to create middleware in three easy steps. You can download the project here.

You don't need a controller to access static files.

like image 136
Clint B Avatar answered Oct 07 '22 01:10

Clint B