为了让用户和网站管理员对我的申请,我决定这条航线上查看/添加/编辑/删除数据:减少重复的代码在控制器动作
routes.MapRoute("ClientRoute",
"{account}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
导致路线,如:mvcapp.net/ 1234 /接触/添加。
为了让用户{管理员除外}访问其他客户端的数据,我在控制器操作中添加了以下代码。
...
var model = repos.GetSomeData();
if (User.IsInRole("Admin") == false) {
if (account == Profile["Client"])
return View(model);
else
return View("WrongClient");
}
...
这样做的最好方法是什么?
解我
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"};
}
}
}
}
HttpContext.Profile – 2009-11-09 19:48:59
在这种情况下,我建议只检查“客户端”是否与用户的id相匹配(假设您使用clientid作为User.Identity.Name) – Omar 2009-11-09 23:03:08
Paul Balmire的回复(第5或第6条)让我知道解决方案我选择。 – 2009-11-11 19:30:24