title: How can I use Dependency Injection in a .Net Core ActionFilterAttribute?
body: <p><strong>AuthenticationRequiredAttribute Class</strong></p>
  <pre><code>public class AuthenticationRequiredAttribute : ActionFilterAttribute
  {
      ILoginTokenKeyApi _loginTokenKeyApi;
      IMemoryCache _memoryCache;
  
      public AuthenticationRequiredAttribute(IMemoryCache memoryCache)
      {
          _memoryCache = memoryCache;
  
          _loginTokenKeyApi = new LoginTokenKeyController(new UnitOfWork());
      }
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var memory = _memoryCache.Get(Constants.KEYNAME_FOR_AUTHENTICATED_PAGES);
  
          string requestedPath = filterContext.HttpContext.Request.Path;
  
          string tokenKey = filterContext.HttpContext.Session.GetString(&quot;TokenKey&quot;)?.ToString();
  
          bool? isLoggedIn = _loginTokenKeyApi.IsLoggedInByTokenKey(tokenKey).Data;
  
          if (isLoggedIn == null ||
              !((bool)isLoggedIn) ||
              !Constants.AUTHENTICATED_PAGES_FOR_NORMAL_USERS.Contains(requestedPath))
          {
              filterContext.Result = new JsonResult(new { HttpStatusCode.Unauthorized });
          }
      }
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
      }
  }
  </code></pre>
  <p><strong>HomeController</strong></p>
  <pre><code>public class HomeController : Controller
  {
      IUserApi _userApi;
      ILoginTokenKeyApi _loginTokenKey;
      IMemoryCache _memoryCache;
  
      public HomeController(IUserApi userApi, ILoginTokenKeyApi loginTokenKey, IMemoryCache memoryCache)
      {
          _loginTokenKey = loginTokenKey;
          _userApi = userApi;
  
          _memoryCache = memoryCache;
      }
  
      [AuthenticationRequired] // There is AN ERROR !!
      public IActionResult Example()
      {
          return View();
      }
  }
  </code></pre>
  <p>ERROR :</p>
  <blockquote>
  <p>Error CS7036  There is no argument given that corresponds to the
  required formal parameter 'memoryCache' of
  'AuthenticationRequiredAttribute.AuthenticationRequiredAttribute(IMemoryCache)'   Project.Ground.WebUI</p>
  </blockquote>
  <p><strong>My problem is actually : I cant use dependency injection in attribute classes</strong>.</p>
  <p>I want to use that attribute without any parameter. Is there any solution to solve it? I use dependency injection but it cant be used for attributes. How I can use it?</p>