Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Injected Object in ASP.NET vNext filter

Tags:

asp.net-core

I'm trying to create my custom authorize attribute, but in asp.net vnext using the default dependency injection framework I don't how to get the injected object. I need to get the injected object in the default ctor.

   public  class CustomAttribute
{

   private IDb _db;

   public CustomAttribute()
   {
       _db = null; // get injected object
   }

   public CustomAttribute(IDb db)
   {
       _db = db;
   }

   // apply all authentication logic
}
like image 367
Son_of_Sam Avatar asked Nov 14 '14 23:11

Son_of_Sam


People also ask

What is dependency injection in ASP NET Vnext?

In ASP.NET vNext, dependency injection is a first class citizen. While in the previous versions of the framework, DI was partially supported, in ASP.NET vNext it is available throughout the entire stack. A minimalistic DI container is provided out of the box but we are leaving the door open to BYOC (Bring Your Own Container).

Is it possible to inject dependencies into an action filter attribute?

Sometimes these filters need to use other components but attributes are quite limited in their functionality and dependency injection into an attribute is not directly possible. This post looks at a few different techniques for injecting dependencies into action filters in ASP.NET Core.

How to inject components into action filters?

Injecting components into action filter attributes directly is not possible but there are various workarounds to allow us to effectively accomplish the same thing. Using ServiceFilter is a relatively clean way to allow dependency injection into individual action filters.

Can dependency injection be used in MVC controller actions?

It is quite common to decorate ASP.NET MVC controller actions with filter attributes to separate cross cutting concerns from the main concern of the action. Sometimes these filters need to use other components but attributes are quite limited in their functionality and dependency injection into an attribute is not directly possible.


1 Answers

You can use the ServiceFilterAttribute for this purpose. The service filter attribute lets the DI system take care of instantiating and maintaining the lifetime of the filter CustomAuthorizeFilter and its any required services.

Example:

// register with DI
services.AddScoped<ApplicationDbContext>();
services.AddTransient<CustomAuthorizeFilter>();

//------------------

public class CustomAuthorizeFilter : IAsyncAuthorizationFilter
{
    private readonly ApplicationDbContext _db;

    public CustomAuthorizeFilter(ApplicationDbContext db)
    {
        _db = db;
    }

    public Task OnAuthorizationAsync(AuthorizationContext context)
    {
        //do something here    
    }
}

//------------------

[ServiceFilter(typeof(CustomAuthorizeFilter))]
public class AdminController : Controller
{
    // do something here
}
like image 86
Kiran Avatar answered Sep 24 '22 01:09

Kiran