<img src="~/static/img1.jpg" />
I used this article to enable static files and these code doesn't work.
[Authorize]
public IActionResult BannerImage()
{
var file = Path.Combine(Directory.GetCurrentDirectory(),
"MyStaticFiles", "images", "banner1.svg");
return PhysicalFile(file, "image/svg+xml");
}
Also another article didn't help. But I don't know how to make images available only to authorized users?
UPDATE
I added to Configure the WWWROOT as a static directory to set cache headers
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "WWWROOT"))
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
OnPrepareResponse = x =>
{
if (x.Context.User.Identity.IsAuthenticated)
{
return;
}
x.Context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
}
});
Then authorization stop working.