답변

1

roles 특성은 ASP.NET과의 상호 운용성을위한 것이며 MVC 전용 응용 프로그램에서 사용하면 안됩니다.

MVC의 경우 컨트롤러 작업에 AuthorizeAttribute를 이미 정의한 경우 security trimming이 활성화되어 있으면 MvcSiteMapProvider가 자동으로 해당 노드를 선택하고 일치하는 노드를 숨 깁니다.

[Authorize] 
public ActionResult Course() 
{ 
    return View(); 
} 

[Authorize] 
[HttpPost] 
public ActionResult Course(CourseModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // Implementation omitted 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

기본 AuthorizeAttribute 역할을 수락하지만, 역할 속성과 동일한 방식으로 작동합니다 - 사용자에 그것을 성공시킬 것 즉, 어떤 역할을.

그러나 AuthorizeAttribute를 직접 상속 받아 IsAuthorized method을 재정 의하여 필요에 따라 논리를 변경할 수 있습니다.

public class SpecialAuthorizeAttribute : AuthorizeAttribute 
{ 
    private string _requiredRoles; 
    private string[] _requiredRolesSplit = new string[0]; 

    /// <summary> 
    /// Gets or sets the required roles. The user must be a member of all roles for it to succeed. 
    /// </summary> 
    /// <value> 
    /// The roles string. 
    /// </value> 
    /// <remarks>Multiple role names can be specified using the comma character as a separator.</remarks> 
    public string RequiredRoles 
    { 
     get { return _requiredRoles ?? String.Empty; } 
     set 
     { 
      _requiredRoles = value; 
      _requiredRolesSplit = SplitString(value); 
     } 
    } 

    /// <summary> 
    /// Determines whether access for this particular request is authorized. This method uses the user <see cref="IPrincipal"/> 
    /// returned via <see cref="HttpRequestContext.Principal"/>. Authorization is denied if the user is not authenticated, 
    /// the user is not in the authorized group of <see cref="Users"/> (if defined), or if the user is not in any of the authorized 
    /// <see cref="Roles"/> (if defined). 
    /// </summary> 
    /// <param name="actionContext">The context.</param> 
    /// <returns><c>true</c> if access is authorized; otherwise <c>false</c>.</returns> 
    protected override bool IsAuthorized(HttpActionContext actionContext) 
    { 
     if (actionContext == null) 
     { 
      throw new ArgumentNullException("actionContext"); 
     } 

     IPrincipal user = actionContext.ControllerContext.RequestContext.Principal; 
     if (user == null || user.Identity == null || !user.Identity.IsAuthenticated) 
     { 
      return false; 
     } 

     // Ensure all of the roles in RequiredRoles are present. 
     if (_requiredRolesSplit.Length > 0 && !_requiredRolesSplit.All(user.IsInRole)) 
     { 
      return false; 
     } 

     // Call the base class to check the users and roles there. 
     return base.IsAuthorized(actionContext); 
    } 

    /// <summary> 
    /// Splits the string on commas and removes any leading/trailing whitespace from each result item. 
    /// </summary> 
    /// <param name="original">The input string.</param> 
    /// <returns>An array of strings parsed from the input <paramref name="original"/> string.</returns> 
    internal static string[] SplitString(string original) 
    { 
     if (String.IsNullOrEmpty(original)) 
     { 
      return new string[0]; 
     } 

     var split = from piece in original.Split(',') 
        let trimmed = piece.Trim() 
        where !String.IsNullOrEmpty(trimmed) 
        select trimmed; 
     return split.ToArray(); 
    } 
} 

그런 다음 새 속성을 사용하여 필요한 역할을 지정할 수 있습니다.

[SpecialAuthorize(RequiredRoles = "CORLIC, VIEWCOBO")] 
public ActionResult Course() 
{ 
    return View(); 
} 

[SpecialAuthorize(RequiredRoles = "CORLIC, VIEWCOBO")] 
[HttpPost] 
public ActionResult Course(CourseModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // Implementation omitted 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

또 다른 가능한 옵션은 shown here으로 FluentSecurity을 사용하는 것입니다. FluentSecurity v2.0이 MvcSiteMapProvider와 함께 작동하려면 HandleSecurityAttribute code을 프로젝트에 복사하고 속성 대신 AuthorizeAttribute에서 상속하도록 변경 한 다음 FluentSecurity 설명서에 지정된대로 사용하십시오.