Web Api自定义授权属性的属性

11

我正在尝试扩展默认的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 属性中使用的 Debug 窗口中显示如下:

set:26833123 True set:39602703 True get:43424763 False get:43424763 False get:43424763 False // 这应该是“True”的调用...

我注意到了两件事:

  • TypeId(属性的唯一标识符)在具有 BypassValidation = true 和具有 BypassValidation = false 的调用之间不同。
  • id“ 43424763”没有相应的设置

有什么想法吗?

谢谢,Joao


为什么在Web Api授权属性中要使用MVC标签? - Léon Pelletier
2个回答

12

Web API的工作方式是对父级范围(在这种情况下是控制器)调用authorize属性,并且需要手动执行覆盖(在操作中覆盖authorize属性)。如果我错了,请纠正我。

因此,解决方案可能如下:

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

什么是actionAttributes?它是authorizeAttributes吗? - Omid.Hanjani
不,这是在操作上使用的属性列表。 - JCS

3
有点晚了,但对于其他遇到类似问题的用户:在Web API 2中,您可以使用"OverrideAuthorization"覆盖所有先前的授权属性(全局授权过滤器、控制器授权属性等),然后只需使用Authorize属性,而无需指定角色。 Authorize属性的默认行为仅检查用户是否已经认证。在这种情况下:
[YourCustomAuthorize]
public class UserProfileController : ApiController
{
    [OverrideAuthorization]
    [Authorize]
    public bool Verify(string verificationCode)
    {
    // TODO
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接