简体   繁体   English

ASP.NET 核心自定义授权 & Memory 缓存

[英]ASP.NET Core Custom Authorization & Memory Cache

I am implementing a program that Caches all Roles by combining them with CustomAuthorize.我正在实现一个程序,通过将它们与 CustomAuthorize 组合来缓存所有角色。 It is a page-based authorization.它是基于页面的授权。 In the customAuthorize Class I couldn't figure out how to access CacheHelper.cs via ICacheHelper.在 customAuthorize Class 中,我无法弄清楚如何通过 ICacheHelper 访问 CacheHelper.cs。

This is CacheHelperClass这是 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这是 ICacheHelper class

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

Now, CustomAuthorize where I stuck.现在,CustomAuthorize 我卡住了。

 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;启动.cs;

  public void ConfigureServices(IServiceCollection services)
    {

        services.AddTransient<ICacheHelper, CacheHelper>();
        services.AddMemoryCache();

For resolving dependencies in an AuthorizeAttribute you can use the AuthorizationFilterContext to get your services.为了解决AuthorizeAttribute中的依赖关系,您可以使用AuthorizationFilterContext来获取您的服务。

You can use the following snippet in CustomAuthorizeAttribute :您可以在CustomAuthorizeAttribute中使用以下代码段:

public void OnAuthorization(AuthorizationFilterContext authorizationFilterContext)
{
    var cacheHelper = authorizationFilterContext.HttpContext.RequestServices.GetService(typeof(ICacheHelper)) as ICacheHelper;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM