Memflex icon indicating copy to clipboard operation
Memflex copied to clipboard

How to use Unity instead of Ninject as IoC?

Open Rotte2 opened this issue 11 years ago • 5 comments

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

Rotte2 avatar Sep 12 '13 17:09 Rotte2

Hi Rotte2:

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

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.

OdeToCode avatar Sep 13 '13 12:09 OdeToCode

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<UserProfile>, FlexMembershipProvider<UserProfile>>(new HierarchicalLifetimeManager()) .RegisterType<IFlexRoleProvider, FlexRoleProvider>(new HierarchicalLifetimeManager()) .RegisterType<IFlexOAuthProvider<UserProfile>, FlexMembershipProvider<UserProfile>>(new HierarchicalLifetimeManager()) .RegisterType<IFlexUserStore<UserProfile>, FlexMembershipUserStore<UserProfile, Role>>(new HierarchicalLifetimeManager()) .RegisterType<IFlexRoleStore>(new InjectionFactory(c => (IFlexRoleStore)container.Resolve<IFlexUserStore<UserProfile>>()));

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

Rotte2 avatar Sep 13 '13 18:09 Rotte2

@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.

OdeToCode avatar Sep 14 '13 13:09 OdeToCode

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 [email protected]

@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/OdeToCode/Memflex/issues/35#issuecomment-24445100 .

Rotte2 avatar Sep 18 '13 14:09 Rotte2

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...

OdeToCode avatar Sep 18 '13 21:09 OdeToCode