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?
Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.
Using IConfiguration The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.
Filters in ASP.NET Core allow code to run before or after specific stages in the request processing pipeline. Built-in filters handle tasks such as: Authorization, preventing access to resources a user isn't authorized for. Response caching, short-circuiting the request pipeline to return a cached response.
A filter can be added to the pipeline at one of three scopes: by action method, by controller class or globally (which be applied to all the controller and actions). We need to register filters in to the MvcOption. Filters collection within ConfigureServices method. // Add framework services.
You can use ServiceFilter attribute
, more info in this blog post and asp.net docs.
[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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With