You can create such an attribute using MVC filters.
For example create a filter like this:
public class CheckHeaderFilter : Attribute, IResourceFilter
{
private readonly string[] _headers;
public CheckHeaderFilter(params string[] headers)
{
_headers = headers;
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
if (_headers == null) return;
if (!_headers.All(h => context.HttpContext.Request.Headers.ContainsKey(h)))
{
//do whatever you need to do when check fails
throw new Exception("Necessary HTTP headers not present!");
}
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
and then use it on an action (or controller):
[CheckHeaderFilter(HeaderOptions.RequestFor)]
public IActionResult Index()
{
...
}
I strongly recommend you read through the docs, so you know which type of filter to use. In this example I used ResourceFilter as it's fairly early in the pipeline (right after auth and before model binding - which makes sense for your scenario).
But depending on what you need to do you should use the appropriate filter.