简体   繁体   中英

Implement FluentSecurity 2.0.0 with Ninject MVC

Error activating ISecurityContext using binding from ISecurityContext to SecurityContext.

I'm getting the above error with FluentSecurity 2.0.0 when I'm trying to configure it with Ninject.Web.Mvc3 in an ASP.NET MVC 4 web application.

I think the internal IoC of FluentSecurity and the Ninject IoC may be clashing. Or I may be incorrectly setting up the DependencyResolver in the SecurityConfigurator.

I need to set it up with IoC as I need to get the UserRoles through an injected class.

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;
    }
}

Where am I going wrong? Is there another way I could achieve this?

I faced the same situation. It happened because Ninject throws an exception when cannot resolve a dependency. I solved it implementing my own 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);
    }
}

I passed the kernel instance in my ninject configuration class

FluentSecurityServiceLocator.Kernel = kernel;

Hope this helps!

I'm not really familiar with Ninject but are you sure that DependencyResolver.Current.GetServices and DependencyResolver.Current.GetService won't throw an exception when FluentSecurity asks for something (like ISecurityContext) that is not registered with Ninject?

In structuremap there is a method called TryGetInstance that won't throw an exception when asking for something that is not registered in the container. You can read more on how FluentSecurity and IoC works here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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