is it possible to somehow only allow authenticated users to view certain images? I'm building a web gallery at the moment, and I dont want non-authenticated users to be able to see the images.
You could put those images somewhere on the server where users don't have access (like for example the ~/App_Data
folder) in order to prevent direct access to them and then use a controller action to serve them. This action will be decorated with an Authorize attribute to allow only authenticated users to call it:
[Authorize]
public ActionResult Image(string name)
{
var appData = Server.MapPath("~/App_Data");
var image = Path.Combine(appData, name + ".png");
return File(image, "image/png");
}
and then:
<img src="@Url.Action("Image", "SomeController", new { name = "foo" })" alt="" />
Inside the view you could also test whether the user is authenticated before displaying the image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With