OdeToCode/Memflex

How to use Unity instead of Ninject as IoC?

Rotte2 opened this issue · 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

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.

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

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

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
.

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