ASP.NET MVC - Image Only + Verified Users Only

Is it possible in any way to allow authenticated users to view certain images? I am creating a web gallery at the moment and I do not want unidentified users to see images.

+4
source share
2 answers

You can place these images somewhere on a server where users do not have access (for example, to the ~/App_Data ) to prevent direct access to them, and then use the controller action to serve them. This action will be decorated with the Authorize attribute to allow only its authorized users:

 [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 can also check if the user is checked before displaying the image.

+10
source

Yes, you can hide images from non-authenticated users.

Depending on how your images are displayed, you can hide them through

1. Using the Authorize attribute in a controller action

2. HTML expose in a view that displays images in

 if (User.Identity.IsAuthenticated) { // image HTML here } 

You will want to place the images somewhere where they cannot be viewed without authentication, for example, in App_Data , as Darin suggests.

0
source

Source: https://habr.com/ru/post/1347371/


All Articles