사용자와 사이트 관리자가 내가이 길에 결정 내 응용 프로그램에서 데이터를보기/추가/수정/삭제 할 수 있도록하려면 컨트롤러 작업에서 중복 코드를 줄 mvcapp.net/을 1234/contact/add.는
{다른 관리자의} 사용자가 다른 클라이언트의 데이터에 액세스하지 못하게하려면 컨트롤러 동작에 다음 코드를 추가해야합니다.
...
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 –
그런 경우 "client"가 사용자 ID와 일치하는지 확인하는 것이 좋습니다 (User.Identity.Name으로 clientid를 사용한다고 가정). – Omar
Paul Balmire의 응답 (5 번째 또는 6 번째 항목) 내가 선택한 솔루션. –