Я реализую пользовательский фильтр аутентификации и использую подход «Пассивные атрибуты», описанный здесь: http://blog.ploeh.dk/2014/06/13/passive-attributes/
DI работает, как и ожидалось, но я не могу понять, как читать пользовательские атрибуты из самого контроллера?Я хотел бы, чтобы как отдельные действия, так и целые контроллеры поддерживали эту функцию.
Пример контроллера:
[TokenAuth] // This attribute not "visible"
public class SupportController : ApiController
{
private ISecurityService SecurityService { get; }
public SupportController(ISecurityService securityService)
{
this.SecurityService = securityService;
}
[TokenAuth] // This attribute works
[HttpGet]
public object StartupData()
{
return "Startup data";
}
}
Это часть кода фильтра, где я читаю пользовательский атрибут:
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
var tokenAuthAttribute = actionContext.ActionDescriptor.GetCustomAttributes<TokenAuthAttribute>(true).SingleOrDefault();
// This line below exists unless attribute placed on method/action
if (tokenAuthAttribute == null) return continuation();
var req = actionContext.Request;
Есть ли способ получить доступ к атрибутам контроллера?