1

Я настроил свое меню с помощью MVCSiteMap и у меня есть этот узел:MVCSiteMap Узел требует- несколько ролей

<mvcSiteMapNode title="Courses Form" controller="Booking" action="Course" roles="CORLIC, VIEWCOBO"/> 

Я пытаюсь применять, что этот узел должен иметь роль «CORLIC» И «VIEWCOBO» для он должен быть виден, но, конечно, это означает, что он будет отображаться, если пользователь имеет одно из указанных выше.

Возможно ли это?

Спасибо.

ответ

1

Атрибут ролей предназначен для взаимодействия с ASP.NET и не должен использоваться в приложении MVC.

Для MVC, если вы уже определили AuthorizeAttribute в действиях вашего контроллера, MvcSiteMapProvider автоматически подберет их и спрячет соответствующие узлы соответственно, если включен security trimming.

[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); 
} 

Другой возможный вариант заключается в использовании в качестве FluentSecurityshown here. Для FluentSecurity v2.0 для работы с MvcSiteMapProvider вам необходимо скопировать HandleSecurityAttribute code в ваш проект и изменить его для наследования из AuthorizeAttribute вместо атрибута, а затем использовать его, как указано в документации FluentSecurity.

Смежные вопросы