【问题标题】:Implement FluentSecurity 2.0.0 with Ninject MVC使用 Ninject MVC 实现 FluentSecurity 2.0.0
【发布时间】:2013-09-12 12:10:21
【问题描述】:
Error activating ISecurityContext using binding from ISecurityContext to SecurityContext.

当我尝试在 ASP.NET MVC 4 Web 应用程序中使用 Ninject.Web.Mvc3 配置 FluentSecurity 2.0.0 时,我遇到了上述错误。

我认为 FluentSecurity 的内部 IoC 和 Ninject IoC 可能会发生冲突。或者我可能在 SecurityConfigurator 中错误地设置了 DependencyResolver。

我需要使用 IoC 进行设置,因为我需要通过注入的类获取 UserRoles。

public static class SecurityConfig
{
    public static ISecurityConfiguration Configure()
    {
        SecurityConfigurator.Configure(
            configuration =>
                {
                    configuration.ResolveServicesUsing(
                        DependencyResolver.Current.GetServices, 
                        DependencyResolver.Current.GetService);

                    configuration.DefaultPolicyViolationHandlerIs(() => new DefaultPolicyViolationHandler());

                    configuration.GetAuthenticationStatusFrom(
                        () => HttpContext.Current.User.Identity.IsAuthenticated);

                    configuration.GetRolesFrom(
                        () =>
                        ((IPersonManager)DependencyResolver
                        .Current
                        .GetService(typeof(IPersonManager)))
                        .GetCurrentUserRoles());

                    configuration.ForAllControllers().DenyAnonymousAccess();
                    configuration.For<AdminController>().RequireAnyRole(Role.Administrator);
                });

        return SecurityConfiguration.Current;
    }
}

我哪里出错了?还有其他方法可以实现吗?

【问题讨论】:

    标签: asp.net-mvc-4 ninject.web.mvc fluent-security


    【解决方案1】:

    我遇到了同样的情况。发生这种情况是因为 Ninject 在无法解析依赖项时抛出异常。我解决了它实现我自己的 ISecurityServiceLocator

    public class FluentSecurityServiceLocator : ISecurityServiceLocator
    {
        public static IKernel Kernel { get; set; }
    
        public object Resolve(Type typeToResolve)
        {
            return Kernel.TryGet(typeToResolve);
        }
    
        public IEnumerable<object> ResolveAll(Type typeToResolve)
        {
            if (!Kernel.GetBindings(typeToResolve).Any())
            {
                return new List<object>();
            }
            return Kernel.GetAll(typeToResolve);
        }
    }
    

    我在 ninject 配置类中传递了内核实例

    FluentSecurityServiceLocator.Kernel = kernel;
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我对 Ninject 不是很熟悉,但您确定当 FluentSecurity 请求未在 Ninject 注册的内容(如 ISecurityContext)时,DependencyResolver.Current.GetServicesDependencyResolver.Current.GetService 不会引发异常吗?

      在结构映射中有一个名为 TryGetInstance 的方法,当请求未在容器中注册的内容时,它不会抛出异常。您可以在此处阅读有关 FluentSecurity 和 IoC 工作原理的更多信息:

      https://github.com/kristofferahl/FluentSecurity/wiki/IoC-container-integration

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多