1

I am implementing a program that Caches all Roles by combining them with CustomAuthorize. It is a page-based authorization. In the customAuthorize Class I couldn't figure out how to access CacheHelper.cs via ICacheHelper.

This is CacheHelperClass

public class CacheHelper : ICacheHelper
{
    private readonly IMemoryCache memCache;
    private readonly IConfiguration configuration;
    public CacheHelper(IServiceProvider serviceProvider, IConfiguration configuration)
    {
        this.memCache = (IMemoryCache)serviceProvider.GetService(typeof(IMemoryCache));
        this.configuration = configuration;
    }

    public async Task<List<RoleTreeListViewModel>> GetRoleTreeListViewModel()
    {
        var cacheList = memCache.Get<List<RoleTreeListViewModel>>("RolTree");
        if (cacheList == null)
        {
            var model_list_result =
                await new RoleTreeRepository(configuration).GetAllRoleTreeForAuthorization();
            cacheList = model_list_result;
            memCache.Set("RolTree", model_list_result);
        }
        return cacheList;
    }

This is ICacheHelper class

 public interface ICacheHelper
{
    public Task<List<RoleTreeListViewModel>> GetRoleTreeListViewModel();

Now, CustomAuthorize where I stuck.

 public class CustomAuthorizeAttribute : AuthorizeAttribute, IAuthorizationFilter
{
    private  ICacheHelper cacheHelper;        
    public void OnAuthorization(AuthorizationFilterContext authorizationFilterContext)
    {
        cacheHelper = new CacheHelper()//I cannot call it here


        cacheHelper.GetRoleTreeListViewModel();
        Task<List<RoleTreeListViewModel>> oParentRoleTreeList =  cacheHelper.GetRoleTreeListViewModel();
        Task<List<Item>> oParentItemList =  cacheHelper.GetItemList();
        Task<List<Role_ItemListViewModel>> oParentRoleItemList =  cacheHelper.GetRole_ItemList();
        string filePath = authorizationFilterContext.HttpContext.Request.Path;
        string hataSayfasi = "~/Account/Login?returnUrl=" + filePath;
        string queryString = string.Empty;
        var request = authorizationFilterContext.HttpContext.Request;

Startup.cs;

  public void ConfigureServices(IServiceCollection services)
    {

        services.AddTransient<ICacheHelper, CacheHelper>();
        services.AddMemoryCache();
1
  • 1
    Could you try to get the CacheHelper as follows: var cacheHelper = authorizationFilterContext.HttpContext.RequestServices.GetService(typeof(ICacheHelper)) as ICacheHelper Commented May 15, 2021 at 7:59

1 Answer 1

1

For resolving dependencies in an AuthorizeAttribute you can use the AuthorizationFilterContext to get your services.

You can use the following snippet in CustomAuthorizeAttribute:

public void OnAuthorization(AuthorizationFilterContext authorizationFilterContext)
{
    var cacheHelper = authorizationFilterContext.HttpContext.RequestServices.GetService(typeof(ICacheHelper)) as ICacheHelper;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.