You can’t block the URL
. In this case, you have to do the control within your Action. If the logged-in user is the 1 (by Session, cookie or any form of control you have), and try to access the 2, you check if it is the logged-in user is the same who is trying to fetch the information, if it is not you go to another Action. Example below:
[Authorize]
public ActionResult Usuario(int id)
{
var usuarioLogado = UsuarioServico.SessaoUsuarioLogado();
// Sua logica de validação, eu uso session
if (id != usuarioLogado.UsuarioID)
{
return RedirectToAction("Sem Acesso", "Usuario");
}
using (var db = new Conexao())
{
var usuario = db.Usuario.Find(id);
return View(usuario);
}
}
Function Login User Login
public static UsuarioLogadoDTO SessaoUsuarioLogado()
{
return HttpContext.Current.Session[Constante.sessaoUsuarioLogado] as UsuarioLogadoDTO;
}
Since your verification has to be done on top of data, I see no alternative but to do an if and check the Action id with the logged-in user id and fire an Exception if it doesn’t. Usually the rules are made on top of actions or controllers, as yours is done on top of data you will need to check this inside the controller and fire the Exception.
– Thiago Silva