【发布时间】: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