2013-10-14 3 views
1

모든 작업 메서드에 대해 mvc3에서 역할 기반 권한 부여를 구현하는 방법을 알려줄 수 있습니까? 지금은 내 응용 프로그램에서 사용자 역할을 추적하는 코드를 작성하지 않았습니다.mvc3의 역할 기반 권한

메뉴를 빌드하는 역할을 확인하는 iam의 주 메뉴에서만, 사용자가 url을 입력 할 때 사용자의 액세스를 거부해야합니다. 속성을 구현할 생각이었습니다. 누구든지 제안을 할 수 있습니까?

미리 감사드립니다.

+0

논리를 OnAuthorization 인증 필터에 기록하고 기본 컨트롤러에 넣고 기본 컨트롤러를 각 컨트롤러에 상속합니다. –

답변

0

시도해보십시오.

protected override void OnAuthorization(AuthorizationContext filter_context) 
{ 
    #region If auth cookie is present 
    if (auth_cookie != null) 
    { 
     #region IF loggedin user is a member 
     if (SiteUsers.LoggedInUser.UserRole == UserRole.Buyer 
      && filter_context.ActionDescriptor.ControllerDescriptor.ControllerName == "Home" 
      && filter_context.ActionDescriptor.ActionName == "Index") 
     { 
      filter_context.Result = RedirectToAction("Index", "Home"); 
     } 
     #endregion 

     #region If loggedin user is a super admin 
     else if (SiteUsers.LoggedInUser.UserRole == UserRole.Administrator && !filter_context.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(Adminstrator), false).Any()) 
     { 
      if (!filter_context.ActionDescriptor.GetCustomAttributes(typeof(AllowAdmin), false).Any()) 
      { 
       filter_context.Result = RedirectToAction("Home", "Admin"); 
      } 

     } 
     #endregion 

     ViewBag.SiteUsers = SiteUsers; 
    } 
    #endregion 

    #region if authorization cookie is not present and the action method being called is not marked with the [SkipAuthentication] attribute 
    else if (!filter_context.ActionDescriptor.GetCustomAttributes(typeof(SkipAuthentication), false).Any()) 
    { 
     if (Request.IsAjaxRequest()) filter_context.Result = Json(new ActionOutput { Results = new List<string> { Url.Action("Signin", "Home") }, Status = ActionStatus.Error }, JsonRequestBehavior.AllowGet); 
     else 
      filter_context.Result = RedirectToAction("Signin", "Home"); 
    } 
    #endregion 

    #region if authorization cookie is not present and the action method being called is marked with the [SkipAuthentication] attribute 
    else 
    { 
     SiteUsers = new ReplictivityUserDetails(); 
     ViewBag.SiteUsers = SiteUsers; 
    } 
    #endregion 
}