简体   繁体   中英

FluentSecurity and Ninject

Error activating IntPtr

I'm trying to configure FluentSecurity (v.1.4) with Ninject (v.3) in an ASP.NET MVC 4 application.

I can't set up the ResolveServicesUsing() configuration expression without throwing the above error.

SecurityConfigurator.Configure(
    configuration =>
        {
            configuration.ResolveServicesUsing(
                DependencyResolver.Current.GetServices, 
                DependencyResolver.Current.GetService);
...

I've also tried using another overload for ResolveServicesUsing()

configuration.ResolveServicesUsing(
    type => DependencyResolver.Current.GetServices(type));

FluentSecurity needs to be configured with Ninject to inject the method for finding my users' roles and also for the PolicyViolationHandler implementations.

UPDATE

I've found I can leave out the offending lines and still have my GetRolesFrom() implementation called (hurrah):

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

I still can't get my PolicyViolationHandler to work, however:

public class RequireRolePolicyViolationHandler : IPolicyViolationHandler
{
    public ActionResult Handle(PolicyViolationException exception)
    {
        return new RedirectToRouteResult(
            new RouteValueDictionary(
                new
                    {
                        action = "AccessDenied", 
                        controller = "Home"
                    }));
    }
}

I'm doing the binding in a NinjectModule like this:

public class SecurityModule : NinjectModule
{
    public override void Load()
    {
        this.Kernel.Bind<IPolicyViolationHandler>()
                   .To<RequireRolePolicyViolationHandler>();
    }
}

Error activating IntPtr

Unfortunately you havn't posted the complete StackTrace. But usually you will get this exception when injecting a Func to some class without having a binding or using the Factory extension.

I use Fluent Security with Ninject as IOC container.

In your Fluent Security configuration, you need to set the service locator to the NinjectServiceLocator.

    public static void Configure(IKernel kernel)
    {
        var locator = new NinjectServiceLocator(kernel);
        ServiceLocator.SetLocatorProvider(() => locator);

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

     ....
    }

You can get the locator here .

Hope this helps

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