Answer to the second question
The issue with the MembershipProvider
and RoleProvider
is that they are not built by the container, but by the framework. AFAIK the only way to set the active membership provider is the web.config file.
So, if you want to use the container to resolve dependencies like repositories, services etc. via the container, you have to use the good old (I'm kidding) Service Locator pattern.
public class CustomMebershipProvider : ICustomMebershipProvider {
public bool ValidateUser(string user, string pwd) {
var svc = Global.Container.Resolve<IMyService>();
/* ... */
}
}
To the first question
You can still inject the active membership provider in the controllers using Constructor Injection.
First of all, create an interface with all methods you need. The issue here is that MembershipProvider
is a concrete class, so you can't inherit an interface from it. Just create your new interface, copying method signatures of function you need from MembershipProvider
:
public interface ICustomMembershipProvider {
bool MyMethod();
/* From MembershipProvider */
bool ValidateUser(string username, string password);
/* ... */
}
Then let your provider implement it and inject:
Container
.RegisterType<ICustomMembershipProvider>(
new InjectionFactory(c => (ICustomMembershipProvider) Membership.Provider))
Of course, you still have to register your custom membership provider in the web.config.