Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you inject an asp.net mvc3 custom membership provider using Autofac?

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

If I try and inject the provider using the ctor, I get an error: 'No parameterless constructor defined for this object.'

public class MyMemberShipProvider : MembershipProvider
    {

        IUserRepository userRepository;

  public MyMemberShipProvider(IUserRepository userRepository)
        {
            this.userRepository = userRepository;

        }
like image 266
tobias Avatar asked Feb 21 '12 13:02

tobias


1 Answers

You can not inject into the in-built providers (Membership / Roles). You can use the MVC 3 DependencyResolver with Autofac.

A quick example...

public override bool ValidateUser(string username, string password)
{
    var userRepo = DependencyResolver.Current.GetService<IUserRepository>();
    return userRepo.ValidateUser(username, password);
}
like image 134
Neil Kinnish Avatar answered Oct 13 '22 06:10

Neil Kinnish