Home > Blockchain >  APS.NET CORE 5 Web Api - Static files, Authorization and Authentication
APS.NET CORE 5 Web Api - Static files, Authorization and Authentication

Time:03-01

I have static files folder:

enter image description here

And I followed Microsoft documentation to validate authorization and authentication:

enter image description here

enter image description here

My controller does not have [Authorize].

enter image description here

I need to Sign in to receive token, to show files only for authenticated users. I have JWT authentication btw.

What I'm doing wrong? Any help?

CodePudding user response:

You could find the explain in official document:

testresult

And the document also provide an alternative approach to serve files based on authorization is to: Store them outside of wwwroot and any directory accessible to the Static File Middleware. Serve them via an action method to which authorization is applied and return a FileResult object:

[Authorize]
public IActionResult BannerImage()
{
    var filePath = Path.Combine(
        _env.ContentRootPath, "MyStaticFiles", "images", "red-rose.jpg");

    return PhysicalFile(filePath, "image/jpeg");
}
  • Related