سؤال

كيف يمكنني استخدام التخزين المؤقت ربما للمساعدة في هذه السمة؟ في الأساس ، لا تضطر إلى إجراء الكثير من المكالمات إلى Container.getService والحصول على المستخدم. هل يمكنني وضع بعض التخزين المؤقت في مكانه من شأنه أن يخبئ الهوية كمفتاح ومخطط كقيمة وابحث عنها فقط؟

    public class AdminsAndProgramManagersOnlyAttribute : FilterAttribute, IAuthorizationFilter
{
    public AdminsAndProgramManagersOnlyAttribute()
    {
        Order = 1; //Must come AFTER AuthenticateAttribute
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var userRepository = GlobalApplication.container.GetService<IRepository<ProjectPlannerInfo>>();
        var identity = filterContext.HttpContext.User.Identity;
        var planner = userRepository.GetAll().WhereEmailIs(identity.Name);

        if (!planner.IsInRole("Db Admin") || planner.ProgramManager == 1)
        {
            filterContext.Result = new RedirectToRouteResult("error", new RouteValueDictionary(new { controller = "Error", action = "InsufficientPrivileges", reason = "Contract" }));
        }
    }
}

سو ...

شيء من هذا القبيل؟

 public void OnAuthorization(AuthorizationContext filterContext)
    {
        var identity = filterContext.HttpContext.User.Identity;

        if (filterContext.HttpContext.Cache[identity.Name] == null)
        {
            if (filterContext.HttpContext.Cache["repo"] == null)
            {
                filterContext.HttpContext.Cache.Add("repo", GlobalApplication.container.GetService<IRepository<ProjectPlannerInfo>>(), null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }

            var userRepository = filterContext.HttpContext.Cache["repo"] as IRepository<ProjectPlannerInfo>;

            filterContext.HttpContext.Cache.Add(identity.Name, userRepository.GetAll().WhereEmailIs(identity.Name), null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }

        var planner = filterContext.HttpContext.Cache[identity.Name] as ProjectPlannerInfo;

        if (!planner.IsInRole("Db Admin") || planner.ProgramManager == 1)
        {
            filterContext.Result = new RedirectToRouteResult("error", new RouteValueDictionary(new { controller = "Error", action = "InsufficientPrivileges", reason = "Contract" }));
        }
    }
هل كانت مفيدة؟

المحلول

يمكنك استخدام ذاكرة التخزين المؤقت المدمجة:

filterContext.HttpContext.Cache
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top