所以我不确定我是否应该实施 IAuthorizationFilter 或实施 IActionFilter 甚至其他东西。
您应该实施IAuthorizationFilter
:
public class MyAuthorizeAttribute: FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var key = filterContext.HttpContext.Request.QueryString["param_name"];
if (!IsValid(key))
{
// Unauthorized!
filterContext.Result = new HttpUnauthorizedResult();
}
}
private bool IsValid(string key)
{
// You know what to do here => go hit your RavenDb
// and perform the necessary checks
throw new NotImplementedException();
}
}
如果您想在您的自定义操作过滤器中使用依赖注入,您可以查看following article
您可以在其中实现自定义过滤器提供程序 ( IFilterProvider
)。您可以有一个标记属性,您可以在控制器操作上使用它,然后让这个自定义过滤器提供程序简单地查看该操作是否用这个标记属性修饰并应用自定义授权过滤器。
例如:
public class MyAuthorizeAttribute: Attribute
{
}
并且您的授权过滤器只会实现IAuthorizationFilter
,它不会是FilterAttribute
:
public class MyAuthorizationFilter: IAuthorizationFilter
{
private readonly ISomeRepository repository;
public class MyAuthorizationFilter(ISomeRepository repository)
{
this.repository = repository;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
var key = filterContext.HttpContext.Request.QueryString["param_name"];
if (!IsValid(key))
{
// Unauthorized!
filterContext.Result = new HttpUnauthorizedResult();
}
}
private bool IsValid(string key)
{
// You know what to do here => go hit your RavenDb
// and perform the necessary checks
throw new NotImplementedException();
}
}
然后您将拥有自定义过滤器提供程序:
public class MyFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (actionDescriptor.GetCustomAttributes(typeof(MyAuthorizeAttribute), true).Any())
{
var filter = DependencyResolver.Current.GetService<MyAuthorizationFilter>();
yield return new Filter(filter, FilterScope.Global);
}
yield break;
}
}
将在您的Application_Start
:
FilterProviders.Providers.Add(new MyFilterProvider());