How can you introduce a custom asp.net mvc3 membership provider using Autofac?

I have an MVC3 application using Autofac and a custom membership provider.

If I try to enter the provider using ctor, I get the error message: "A constructor without parameters was not defined for this object."

public class MyMemberShipProvider : MembershipProvider { IUserRepository userRepository; public MyMemberShipProvider(IUserRepository userRepository) { this.userRepository = userRepository; } 
+4
source share
3 answers

You cannot enter built-in providers (membership / roles). You can use MVC 3 DependencyResolver with Autofac.

Quick example ...

 public override bool ValidateUser(string username, string password) { var userRepo = DependencyResolver.Current.GetService<IUserRepository>(); return userRepo.ValidateUser(username, password); } 
+7
source

Avoid permissions in the application code (check the user, etc.), as this is an anti-pattern. You want to allow only in your code / low level code.

This is lower for windsor, but the implementation can be easily adjusted. Here is one of them for the castle windsor, but the implementation should be similar. This is a little cleaner as it is allowed when calling GetProvider - which is the glue code here, which avoids the use of the anti-locator pattern of services in actual membership functions (such as ValidateUser)

http://bugsquash.blogspot.com/2010/11/windsor-managed-membershipproviders.html

+1
source

This is because you must also enter your userRepository. Sort of:

 protected override void Load(ContainerBuilder builder) { builder.RegisterType<UserRepository>().As<IUserReposotory>(); builder.RegisterType<MyMembershipProvider>(); } 
-one
source

Source: https://habr.com/ru/post/1397612/


All Articles