我需要通过添加其他属性来修改我的web api 2项目中的用户角色:DateTime StartDate
和DateTime EndDate
。这需要能够在有限的时间内授予用户角色。获取AuthorizeAttribute在web api 2应用程序中启动和过期日期的工作角色?
我需要做些什么才能获得Authorize
属性,如[Authorize(Role="poweruser")]
等,以了解角色日期?
据源(https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/AuthorizeAttribute.cs)此过滤器最终调用IPrincipal.IsInRole
:
protected virtual bool IsAuthorized(HttpActionContext actionContext)
{
...
if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole))
{
return false;
}
return true;
}
看起来我需要继承的IPrincipal
的实施HttpActionContext.ControllerContext.RequestContext.Principal
并以某种方式在生命周期,而不是默认实现地方注入它。
我该怎么做?