草庐IT

c# - MVC 5 : Custom AuthorizeAttribute and Caching

coder 2024-06-03 原文

我试图通过从中派生并覆盖其某些方法来找到实现自定义 System.Web.Mvc.AuthorizeAttribute 的解决方案。
我正在尝试的每一种方法,我都面临着 MVC 5 的默认授权机制中的某些问题,这使我无法正确扩展它。
我已经在 SO 和许多专用资源上对该领域进行了大量研究,但是我无法像我目前的场景那样为这种场景找到可靠的解决方案。

第一个限制:
我的授权逻辑需要额外的数据,例如 Controller 和方法名称以及应用于它们的属性 而不是 HttpContextBase 能够提供的有限部分数据。
例子:

public override void OnAuthorization(AuthorizationContext filterContext)
{
    ...
    var actionDescriptor = filterContext.ActionDescriptor;
    var currentAction = actionDescriptor.ActionName;
    var currentController = actionDescriptor.ControllerDescriptor.ControllerName;

    var hasHttpPostAttribute = actionDescriptor.GetCustomAttributes(typeof(HttpPostAttribute), true).Any();
    var hasHttpGetAttribute = actionDescriptor.GetCustomAttributes(typeof(HttpGetAttribute), true).Any();

    var isAuthorized = securitySettingsProvider.IsAuthorized(
        currenPrincipal, currentAction, currentController, hasHttpPostAttribute, hasHttpGetAttribute);
    ...
}

这就是为什么我不能在 AuthorizeCore() 方法覆盖中实现我的授权逻辑,因为它只获取 HttpContextBase 作为参数,而我需要做出授权决定的是 AuthorizationContext
这导致我将我的授权逻辑置于 OnAuthorization() 方法覆盖,如上例所示。

但是这里我们遇到了第二个限制:
AuthorizeCore() 方法由缓存系统调用以做出授权决定 当前请求是否应该使用缓存的 ActionResult 或相应的 Controller 方法来创建新的 ActionResult
所以我们不能忘记 AuthorizeCore() 而只使用 OnAuthorization()

在这里,我们将回到初始点:
仅当我们需要来自 HttpContextBase 的更多数据时,如何根据 AuthorizationContext 为缓存系统做出授权决策?
有许多后续问题,例如:
  • 我们应该如何正确实现 AuthorizeCore()这个案例?
  • 我应该实现我自己的缓存来让它提供
    足够的数据给授权系统?以及如何做到
    如果是?
  • 或者我应该告别所有 Controller 的缓存
    用我的自定义 System.Web.Mvc.AuthorizeAttribute 保护的方法?
    这里必须要说的是,我将使用我的自定义 System.Web.Mvc.AuthorizeAttribute 作为全局过滤器,这是
    完全告别缓存,如果这个问题的答案
    问题是肯定的。

  • 所以这里的主要问题是:
    处理此类自定义授权和正确缓存的可能方法是什么?

    UPDATE 1(解决一些可能答案的附加信息):
  • 在 MVC 中没有任何保证AuthorizeAttribute 将服务于单个请求。它可以重复使用
    对于许多请求(见
    here 了解更多信息):

    Action filter attributes must be immutable, since they may be cached by parts of the pipeline and reused. Depending on where this attribute is declared in your application, this opens a timing attack, which a malicious site visitor could then exploit to grant himself access to any action he wishes.



    换句话说,AuthorizeAttribute 必须是不可变的
    不得在任何方法调用 之间共享状态。
    此外在AuthorizeAttribute -as-global-filter 场景,单个实例AuthorizeAttribute 用于处理所有请求。
    如果您认为将 AuthorizationContext 保存在请求的 OnAuthorization() 中,那么您就可以在同一请求的后续 AuthorizeCore() 中获取它,那您就错了。
    因此,您将根据其他请求的 AuthorizationContext 对当前请求做出授权决定。
  • 如果缓存层触发了AuthorizeCore(),则OnAuthorization()之前从未为当前请求调用过(请引用AuthorizeAttributeCacheValidateHandler()开始到AuthorizeCore()sources)。
    换句话说,如果要使用缓存的 ActionResult 服务请求,则只会调用 AuthorizeCore() 而不是 OnAuthorization()
    所以在这种情况下你无论如何都无法保存 AuthorizationContext

  • 因此,在AuthorizationContextOnAuthorization() 之间共享AuthorizeCore() 不是选项!

    最佳答案

    OnAuthorization 方法在 AuthorizeCore 方法之前调用。因此,您可以保存当前上下文以供以后处理:

    public class MyAttribute: AuthorizeAttribute
    {
        # Warning - this code doesn't work - see comments
    
        private AuthorizationContext _currentContext;
    
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
             _currentContext = filterContext;
             base.OnAuthorization(filterContext);
        }
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
             // use _currentContext
        }    
    }
    

    编辑

    因为这不会像亚历山大指出的那样工作。第二种选择可能是完全覆盖 OnAuthorization 方法:
            public override void OnAuthorization(AuthorizationContext filterContext)
            {
                if (filterContext == null)
                {
                    throw new ArgumentNullException("filterContext");
                }
    
                if (OutputCacheAttribute.IsChildActionCacheActive(filterContext))
                {
                    throw new InvalidOperationException(MvcResources.AuthorizeAttribute_CannotUseWithinChildActionCache);
                }
    
                bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true)
                                         || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true);
    
                if (skipAuthorization)
                {
                    return;
                }
    
                if (AuthorizeCore(filterContext.HttpContext))
                {
                    HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                    cachePolicy.SetProxyMaxAge(new TimeSpan(0));
    
                    var actionDescriptor = filterContext.ActionDescriptor;
                    var currentAction = actionDescriptor.ActionName;
                    var currentController = actionDescriptor.ControllerDescriptor.ControllerName;
    
                    var hasHttpPostAttribute = actionDescriptor.GetCustomAttributes(typeof(HttpPostAttribute), true).Any();
                    var hasHttpGetAttribute = actionDescriptor.GetCustomAttributes(typeof(HttpGetAttribute), true).Any();
                    // fill the data parameter which is null by default
                    cachePolicy.AddValidationCallback(CacheValidateHandler, new { actionDescriptor : actionDescriptor, currentAction: currentAction, currentController: currentController, hasHttpPostAttribute : hasHttpPostAttribute, hasHttpGetAttribute: hasHttpGetAttribute  });
                }
                else
                {
                    HandleUnauthorizedRequest(filterContext);
                }
            }
    
        private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            // the data will contain AuthorizationContext attributes
            bool isAuthorized = myAuthorizationLogic(httpContext, data);
            return (isAuthorized) ? HttpValidationStatus.Valid : httpValidationStatus.IgnoreThisRequest;
    
        }
    

    关于c# - MVC 5 : Custom AuthorizeAttribute and Caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26605709/

    有关c# - MVC 5 : Custom AuthorizeAttribute and Caching的更多相关文章

    1. c# - 如何在 ruby​​ 中调用 C# dll? - 2

      如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

    2. C# 到 Ruby sha1 base64 编码 - 2

      我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

    3. 基于C#实现简易绘图工具【100010177】 - 2

      C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

    4. ruby - sinatra 框架的 MVC 模式 - 2

      我想开始使用“Sinatra”框架进行编码,但我找不到该框架的“MVC”模式。是“MVC-Sinatra”模式或框架吗? 最佳答案 您可能想查看Padrino这是一个围绕Sinatra构建的框架,可为您的项目提供更“类似Rails”的感觉,但没有那么多隐藏的魔法。这是使用Sinatra可以做什么的一个很好的例子。虽然如果您需要开始使用这很好,但我个人建议您将它用作学习工具,以对您来说最有意义的方式使用Sinatra构建您自己的应用程序。写一些测试/期望,写一些代码,通过测试-重复:)至于ORM,你还应该结帐Sequel其中(imho

    5. c# - C# 中的 Flatten Ruby 方法 - 2

      我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume

    6. ruby - 可以像在 C# 中使用#region 一样在 Ruby 中使用 begin/end 吗? - 2

      我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用

    7. c# - Ruby 等效于 C# Linq 聚合方法 - 2

      什么是Linq聚合方法的ruby​​等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj

    8. c# - 先学什么? - 2

      关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion几年前我去学校学习编程,毕业后我找到了一份系统管理方面的工作,这就是我职业生涯的方向。我想重新开始某种开发,并且一直在“玩”C#和ASP.NET,但我已经听到很多关于其他"new"语言的讨论(新的意思是它们是新的)我)喜欢Ruby和F#。我想我想知道我是否在浪费时间学习主要的MS语言,而不是成为一名通才。很长一段时间没有离开开发社区(如果我曾经离开过的话)让我在潮流中挣扎,我不想落在时代的

    9. c# - 在 C# 中重现 Ruby OpenSSL private_encrypt 输出 - 2

      我有一个简单的Ruby脚本,我用它在某些HTTPheader上执行private_encrypt以签署要发送到ruby​​RESTAPI的Web请求,该API会根据Base64编码字符串测试Base64编码字符串生成而不是解码Base64和解密数据然后测试原始字符串。我使用的脚本是require"openssl"require"base64"path_to_cert=ARGV[0].dupplain_text=Base64.decode64(ARGV[1].dup)private_key=OpenSSL::PKey::RSA.new(File.read(path_to_cert))pu

    10. C# 的 LINQ 用于在 ruby​​ 中等效的集合操作 - 2

      我是ruby​​开发的新手,我目前正在使用rails2.3.11在ruby​​1.8.7中开发一个项目,我想知道这种语言是否有与C#的linq等效的集合操作,例如where子句。谢谢。 最佳答案 Ruby中Linq的where等价于find_all检查documentationfortheEnumerableModule用于其他功能。 关于C#的LINQ用于在ruby​​中等效的集合操作,我们在StackOverflow上找到一个类似的问题: https://

    随机推荐