Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rotte2 opened this issue Sep 12, 2013 · 5 comments
Open

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

Rotte2 opened this issue Sep 12, 2013 · 5 comments

Comments

@Rotte2
Copy link

Rotte2 commented Sep 12, 2013

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

@OdeToCode
Copy link
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.

@Rotte2
Copy link
Author

Rotte2 commented Sep 13, 2013

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

@OdeToCode
Copy link
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.

@Rotte2
Copy link
Author

Rotte2 commented Sep 18, 2013

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

@OdeToCode
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants