1
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>();
    }
}
Boggin
  • 3,251
  • 3
  • 33
  • 48
  • Have you configured MVC's DependencyResolver to point at Ninject? I'm not familiar with Ninject but there is usually a call to DependencyResolver.SetResolver(IDependencyResolver) in app start, where you pass in an implementation that wraps Ninject's methods to resolve service/type registrations. – Sean B Sep 13 '13 at 15:42
  • It certainly seems to be as I can use the DependencyResolver to call another method but as a check I found the source code for the ninject.web.mvc3 build and it looks as if the System.Web.Mvc DependencyResolver is being set: `NinjectMvcHttpApplicationPlugin Start() { DependencyResolver.SetResolver(this.CreateDependencyResolver()); }` – Boggin Sep 16 '13 at 08:56

2 Answers2

1

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.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
0

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

lopezbertoni
  • 3,551
  • 3
  • 37
  • 53