2017-09-20 1 views
0

일부 사용자 지정 검사가 내 응용 프로그램에서 액세스를 결정할 수 있도록 사용자 지정 권한 부여 특성을 만들었습니다.특정 동작에 대한 오버라이드/결합이 아닌 사용자 지정 권한 특성

컨트롤러 수준에서 사용자 지정 auth 특성을 적용한 다음 특정 동작에 추가 액세스를 추가하려고하면 역할이 '추가'방식으로 적용되지 않습니다.

은 최고 관리자 및 관리자에 대한 정식 검사를 타격 한 다음 그 자체 컨설턴트에 대한 점검을 치는 것 같다 : 컨트롤러

// Allow multiple = true so should roll all occurrences in a request into one 
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] 
public class CustomAuthoriseAttribute : AuthorizeAttribute 
{ 
    public CustomAuthoriseAttribute(params string[] roles) 
    { 
     this.Roles = string.Join(",", roles); 
    } 

    /// <summary> 
    /// Custom routines to determine if a request is considered authorised. 
    /// </summary> 
    /// <param name="httpContext"></param> 
    /// <returns></returns> 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) 
     { 
      throw new ArgumentNullException("httpContext"); 
     } 

     var userManager = httpContext.GetOwinContext().GetUserManager<UserManager>(); 

     var user = userManager.FindById(httpContext.User.Identity.GetUserId()); 

     if (user == null) 
     { 
      return false; 
     } 

     // Log the user out as they should not be allowed access 
     if (user.IsDisabled || user.IsDeleted) 
     { 
      httpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
      httpContext.Session.Clear(); 

      return false; 
     } 

     return base.AuthorizeCore(httpContext); 
    } 
} 

사용법 :

사용자 정의 속성을 부여 승인되지 않은 요청이 발생합니다. 왜 그들은 다 대우받지 못하고 있습니까?

+0

코드를 디버그하고 사용자 지정 특성에서 false를 반환하는 위치를 확인 했습니까? –

+0

'base.AuthorizeCore (httpContext) '를 호출 할 때 false로 반환됩니다. 즉, Roles 속성의 항목에 대해'user.IsInRole' 검사를 수행 할 때 실패해야 함을 의미합니다.이 경우에는 all 지정된 롤. – Tomuke

+0

기본 메소드를 호출하기 전에 코드에서'user.IsInRole'을 검사 해 보았습니까? –

답변

0

여러 권한 부여 특성은 논리적 AND를 사용하여 처리됩니다. 각 속성의 결과는 이전 속성과 AND가됩니다. 이 시나리오에서 SomeAction은 최고 관리자 또는 관리자 (컨트롤러 수준 속성을 기반으로 함) 인 사용자 만 액세스 할 수 있으며 액션 수준 속성을 기반으로하는 컨설턴트입니다.

이렇게하는 데는 몇 가지 방법이 있지만 권한있는 계정 (최고 관리자 및 관리자)과 제한된 계정 (컨설턴트)을 혼합 할 때 컨트롤러 수준의 컨설턴트에게 액세스 권한을 부여하지 않는 것이 좋습니다.

세 가지 역할 모두에서 액세스 할 수있는 새 컨트롤러를 만들어이 작업을이 컨트롤러로 옮깁니다. 그런 다음 원래 컨트롤러에 권한있는 메소드를 남겨 둘 수 있습니다.

[CustomAuthorise(SuperAdministrator, Administrator)] 
public class PrivilegedController : Controller 
{ 

    // Should only accessible by SuperAdministrators and Administrators 
    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

} 

[CustomAuthorise(SuperAdministrator, Administrator, Consultant)] 
public class LessPrivilegedController : Controller 
{ 

    [HttpGet] 
    public ActionResult SomeAction() 
    { 
     return View(); 
    } 
} 
관련 문제