I have this:
[AllowAnonymous]
public FilePathResult GetImage(string user)
{
var path = AppDomain.CurrentDomain.BaseDirectory + "files\\uploads\\users\\" + user + "\\avatar\\";
var ext = this.GetImageExtension(path, user);
return ext != null ? File(path + user + "." + ext, "image/" + ext, user + "." + ext) : File(AppDomain.CurrentDomain.BaseDirectory + "files\\commonFiles\\users\\avatar\\noavatar.png", "image/png", "noavatar.png");
}
And inside my views I have this:
<img src="/MyAccount/GetImage/?user=@User.Identity.Name"
alt="@User.Identity.Name" />
Now, whenever I use this inside my web developer server it works perfectly ok. But when I publish my site on my server, it is not even trying to hit that action. Why?
Because you have hardcoded the url to your controller action instead of using an url helper:
You should never hardcode urls in an ASP.NET MVC application but always use url helpers.
Also passing the currently logged in user as query string parameter looks like a horrible security issue. There’s nothing preventing the user from passing whatever username he likes and consulting the images of that user. You should read the currently authenticated user in your controller action.
So start by getting rid of this query string parameter:
and then in your controller action you could always retrieve the currently logged in user with the
User.Identity.Nameproperty:I’ve also decorated this controller action with the
[Authorize]attribute to make it accessible only to authenticated users. If this is not your case you could still keep the[AllowAnonymous]attribute but check forUser.Identity.IsAuthenticatedbefore attempting to access his username.