Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access IConfiguration in the ActionFilterAttribute in the asp.net core rc2 application [duplicate]

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?

like image 325
vmg Avatar asked Jun 17 '16 07:06

vmg


People also ask

How do I use IConfiguration in .NET Core?

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.

How do I get value from IConfiguration?

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.

What is the use of a filter in the ASP.NET Core application?

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.

How do I register a filter in NET Core?

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.


2 Answers

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);
     }
}
like image 122
tmg Avatar answered Sep 28 '22 18:09

tmg


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;
});
like image 38
adem caglin Avatar answered Sep 28 '22 16:09

adem caglin