【问题标题】:Fluent Security and AOP with CastleDynamicProxy使用 CastleDynamicProxy 实现流畅的安全性和 AOP
【发布时间】:2013-04-26 07:30:21
【问题描述】:

GitHub 上已询问过这个确切的问题,但提供的解决方法似乎在版本 2 中不是可选的。

问题是,如果安全配置正确,即使它是通过 ForAllControllersInheriting 配置的,它也不能与 CastleDynamixProxy 运行时创建的控制器代理一起工作。

我想这是因为当 fluent security 解析规则时这些控制器实际上并不存在。有什么解决方法吗?我想按照GitHub 上的建议创建自定义的 HandleSecurityAttribute,但是我无法使用版本 2 中的所有内部内容来完成它(但这可能是我在 C# 方面不专业的原因,所以我可能只需要提示怎么做)。

我尝试了所有可能的配置:

configuration.ForAllControllersInAssembly(typeof(HomeController).Assembly)
    .DenyAnonymousAccess();
configuration.ForAllControllersInheriting<HomeController>().DenyAnonymousAccess();
configuration.ForAllControllers().DenyAnonymousAccess();

这些似乎都不适用于这个问题。

【问题讨论】:

  • 我已经在 github 上发布了对您的评论的答案。让我知道你需要什么,我相信我们可以想出一些可以解决这个问题的方法。当我们让它工作时,我们可以在这里发布最终结果。

标签: c# asp.net-mvc-4 fluent-security


【解决方案1】:

对于那些仍然想知道如何解决这个问题的人,有一种方法可以在 SecurityHandler 的帮助下使用自定义的授权属性。

public class CastleProxyHandleSecurityAttribute : Attribute, IAuthorizationFilter
{

    private readonly ISecurityHandler securityHandler;

    public CastleProxyHandleSecurityAttribute()
    {
        securityHandler = new SecurityHandler();
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var actionName = filterContext.ActionDescriptor.ActionName;
        var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;

        if (controllerName.StartsWith("Castle") && filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.BaseType != null)
        {
            controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.BaseType.FullName;
        }

        var securityContext = SecurityContext.Current;
        securityContext.Data.RouteValues = filterContext.RouteData.Values;

        var overrideResult = securityHandler.HandleSecurityFor(controllerName, actionName, securityContext);
        if (overrideResult != null) filterContext.Result = overrideResult;
    }
}

【讨论】:

  • 我们将看看是否可以在 FluentSecurity 3.0 中使这变得更容易。感谢您发布解决方案...
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
相关资源
最近更新 更多