Question: I am writing attribute that will verify captcha. In order to work correctly it needs to know secret, which I keep in the settings (Secret manager tool). However I don't know how to read config from the attribute class. DI in asp.net core supports constructor injection (and property injection is not supported), so this will give compilation error:

```
public ValidateReCaptchaAttribute(IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            this.m_configuration = configuration;
        }

```

because when I decorate method with `[ValidateReCaptcha]` I can't pass config

So how do I can read something from config from the method in attribute class?

Answer: 
You can use `ServiceFilter attribute`, more info in this [blog post](https://damienbod.com/2015/09/15/asp-net-5-action-filters/) and [asp.net docs](https://docs.asp.net/en/latest/mvc/controllers/filters.html#configuring-filters).

```
[ServiceFilter(typeof(ValidateReCaptchaAttribute))]
public IActionResult SomeAction()

```

In `Startup`

```
public void ConfigureServices(IServiceCollection services)
{
       // Add functionality to inject IOptions<T>
       services.AddOptions();

       // Add our Config object so it can be injected
       services.Configure<CaptchaSettings>(Configuration.GetSection("CaptchaSettings"));

       services.AddScoped<ValidateReCaptchaAttribute>();
       ...
}

```

And `ValidateReCaptchaAttribute` 

```
public class ValidateReCaptchaAttribute : ActionFilterAttribute
{
     private readonly CaptchaSettings _settings;

     public ValidateReCaptchaAttribute(IOptions<CaptchaSettings> options)
     {
         _settings = options.Value;
     }

     public override void OnActionExecuting(ActionExecutingContext context)
     {
         ...
         base.OnActionExecuting(context);
     }
}

```
You should use `ServiceFilter` like this: 

`[ServiceFilter(typeof(ValidateReCaptcha))]`

And if you want to use `IConfiguration` you should inject it in `ConfigureServices`:

```
services.AddSingleton((provider)=>
{
     return Configuration;
});

```