Asp.net-Mvc

Web Api 自定義授權屬性屬性

  • January 5, 2017

我正在嘗試擴展他預設的 Web Api 授權屬性,以允許經過身份驗證的使用者可以訪問一組操作,即使他們沒有在應用程序中註冊(例如,他們沒有角色)。

public class AuthorizeVerifiedUsersAttribute : AuthorizeAttribute
   {
       /// <summary>
       /// Gets or sets the authorized roles.
       /// </summary>
       public new string Roles { get { return base.Roles; } set { base.Roles = value; } }

       /// <summary>
       ///  Gets or sets the authorized users.
       /// </summary>
       public new string Users { get { return base.Users; } set { base.Users = value; } }

       private bool _bypassValidation;
       /// <summary>
       /// Gets of sets a controller or an action as an authorization exception
       /// </summary>
       public virtual bool BypassValidation
       {
           get
           {
               Debug.WriteLine("get:" + TypeId.GetHashCode() + " " + _bypassValidation);
               return _bypassValidation;
           }
           set
           {
               Debug.WriteLine("set:" + TypeId.GetHashCode() + " " + value);
               _bypassValidation = value;
           }
       }

       protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
       {
           if (HttpContext.Current.User.Identity.IsAuthenticated)
           {
               if (BypassValidation)
               {
                   return true;
               }
               else
               {
                  //return false if user is unverified

               }
           }

           return base.IsAuthorized(actionContext);
       }
   }

它是這樣使用的:

[AuthorizeVerifiedUsers]
public class UserProfileController : ApiController
{

   [AuthorizeVerifiedUsers(BypassValidation = true)]
   public bool Verify(string verificationCode)
   {}
}

到目前為止,此操作是唯一使用 BypassValidation = true 的操作。

出現此問題的原因是該操作的 BypassValidation 屬性為 false,即使在 BypassValidation 屬性中使用的調試視窗顯示以下內容:

set:26833123 True set:39602703 True get:43424763 False get:43424763 False get:43424763 False //呼叫應該有“True”…

我注意到兩件事:

  • TypeId(屬性的唯一標識符)在 BypassValidation = true 的呼叫和 BypassValidation = false 的呼叫之間是不同的。
  • id ‘43424763’ 沒有對應的集合

有任何想法嗎?

在此先感謝,若昂

Web API 的工作方式是為父範圍呼叫授權屬性,在這種情況下是控制器,並且需要手動完成****覆蓋(操作上的授權屬性) (如果我錯了,請糾正我)。

因此,解決方案可能如下所示:

public class AuthorizeVerifiedUsersAttribute : AuthorizeAttribute
{
 (...)

 protected override bool IsAuthorized(HttpActionContext actionContext)
 {

    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
       //retrieve controller action's authorization attributes
       var authorizeAttributes = actionContext.ActionDescriptor.GetCustomAttributes<AuthorizeVerifiedUsersAttribute>();

       //check controller and action BypassValidation value
       if (BypassValidation || 
           actionAttributes.Count > 0 && actionAttributes.Any(x => x.BypassValidation))
       {
           return true;
       }
       else
       {
         //return false if user is unverified
       }

       return base.IsAuthorized(actionContext);
   }
}

引用自:https://stackoverflow.com/questions/20885356