Skip to content

How to use Unity instead of Ninject as IoC? #35

Open

Description

@Rotte2

Hi

It's probaby not the right place for this question, but I would like to use MemFlex with RavenDB in a ASP.NET MVC 4 project of mine. I have not used Ninject as IoC before, and would like to ask, how these should be registered using Unity?

var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Bind<IApplicationEnvironment>().To<AspnetEnvironment>();
kernel.Bind<IFlexMembershipProvider<User>>().To<FlexMembershipProvider<User>>();
kernel.Bind<IFlexRoleProvider>().To<FlexRoleProvider>();
kernel.Bind<IFlexOAuthProvider<User>>().To<FlexMembershipProvider<User>>();
kernel.Bind<IFlexUserStore<User>>().To<FlexMembershipUserStore<User, Role>>();
kernel.Bind<IFlexRoleStore>().ToMethod(c => (IFlexRoleStore)c.Kernel.Get<IFlexUserStore<User>>());
RegisterServices(kernel);

/Michael

Activity

  1. OdeToCode commented on Sep 13, 2013

    @OdeToCode
    Owner

    Hi Rotte2:

    I haven't used Unity, but I'm wondering if it would be as easy as:
    myContainer.RegisterType<IFlexMembershipProvider, FlexMembershipProvider>();

    for all the types above, and in combination with http://www.nuget.org/packages/Unity.Mvc4.

    Sorry I can't provide more detailed help, I'll have to try Unity myself one day.

  2. Rotte2 commented on Sep 13, 2013

    @Rotte2
    Author

    Hi OdeToCode

    A colleague helped me out on this - the final code looks like this (using Unity):

    container.RegisterType<IApplicationEnvironment, AspnetEnvironment>(new HierarchicalLifetimeManager())
    .RegisterType<IFlexMembershipProvider,
    FlexMembershipProvider>(new HierarchicalLifetimeManager())
    .RegisterType<IFlexRoleProvider, FlexRoleProvider>(new HierarchicalLifetimeManager())
    .RegisterType<IFlexOAuthProvider, FlexMembershipProvider>(new HierarchicalLifetimeManager())
    .RegisterType<IFlexUserStore, FlexMembershipUserStore<UserProfile, Role>>(new HierarchicalLifetimeManager())
    .RegisterType(new InjectionFactory(c => (IFlexRoleStore)container.Resolve<IFlexUserStore>()));

    Just in case anyone has the same problem.

    Another question: I want to add more properties to the User entity (like first and last name) - how do I get retrieve the persisted User entity, f.ex. in the Manage method in the Controller class?

    Thanks
    Michael

  3. OdeToCode commented on Sep 14, 2013

    @OdeToCode
    Owner

    @michael:

    Do you have a DbContext derived class setup? You can use that class or go through the DbContext or through FlexUserStore derived class. Let me know if that makes sense.

  4. Rotte2 commented on Sep 18, 2013

    @Rotte2
    Author

    Hi again

    I have a RavenDB setup, and now it's working using the AccountController
    (modified for my needs). That's great. I'm still struggling with the
    Api-controller - to get client requests authenticated (f.ex. when I use
    Fiddler to call my Api-controllers). How is this implemented? And should
    the client provide username:password in the authorization attribute in the
    http header?

    Thx
    Michael ,

    2013/9/14 K. Scott Allen notifications@github.com

    @michael https://github.com/Michael:

    Do you have a DbContext derived class setup? You can use that class or go
    through the DbContext or through FlexUserStore derived class. Let me know
    if that makes sense.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/35#issuecomment-24445100
    .

  5. OdeToCode commented on Sep 18, 2013

    @OdeToCode
    Owner

    Well, there is a fairly broad range of scenarios there. For example, do you expect users to login to the site before script starts calling WebAPI endpoints, or does the WebAPI have to support authentication from clients who aren't logging in via HTML forms?

    I don't know the exact answer to fit your needs, but here are a few pointers.
    http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api
    http://leastprivilege.com/2012/10/26/using-claims-based-authorization-in-mvc-and-web-api/
    http://codebetter.com/johnvpetersen/2012/04/02/making-your-asp-net-web-apis-secure/

    Let me know if that helps...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions