【问题标题】:Web Api Custom Authorize Attribute PropertiesWeb Api 自定义授权属性属性
【发布时间】:2014-01-02 14:42:56
【问题描述】:

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

设置:26833123 真 设置:39602703 真 获取:43424763 错误 获取:43424763 错误 get:43424763 False //应该有“True”的调用...

我注意到两件事:

  • TypeId(属性的唯一标识符)在 BypassValidation = true 的调用和 BypassValidation = false 的调用之间是不同的。
  • id '43424763' 没有对应的集合

有什么想法吗?

提前致谢, 若昂

【问题讨论】:

  • 如果是关于 Web Api 授权属性,为什么要在标签中使用 MVC?

标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api


【解决方案1】:

Web API 的工作方式是为父作用域(在本例中为控制器)调用 authorize 属性,并且需要完成 override(动作上的授权属性)手动(如果我错了,请纠正我)。

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

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吗?
  • 不,是动作上使用的属性列表
【解决方案2】:

有点晚了,但对于其他有类似问题的用户:在 Web API 2 中,您可以使用“OverrideAuthorization”覆盖所有以前的授权属性(全局授权过滤器、控制器授权属性等),然后只需使用 Authorize 属性,没有指定角色。 Authorize 属性的默认行为只是检查用户是否经过身份验证。

在这种情况下:

[YourCustomAuthorize]
public class UserProfileController : ApiController
{
    [OverrideAuthorization]
    [Authorize]
    public bool Verify(string verificationCode)
    {
    // TODO
    }
}

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2020-04-11
    • 2011-07-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多