To allow users and site admins to view/add/edit/delete data in my application I decided on this route:
routes.MapRoute("ClientRoute",
"{account}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
which results in routes like: mvcapp.net/1234/contact/add.
To keep users {except admins} from accessing other client's data I have added the following code in my controller actions.
...
var model = repos.GetSomeData();
if (User.IsInRole("Admin") == false) {
if (account == Profile["Client"])
return View(model);
else
return View("WrongClient");
}
...
What is the best way to do this?
SOLUTION I WENT WITH
public class BaseController : Controller {
protected override OnActionExecuting(ActionExecutingContect filterContext) {
if (filterContext.RouteData.Values["account"] != null) {
string client = filterContext.RouteData.Values["account"].ToString();
if (User.IsInRole("admin") == false) {
if (Profile.Clients.Contains(account) == false)
filterContext.Result = new ViewResult() {ViewName = "WrongClient"};
}
}
}
}