I need to get an instance of the application’s MembershipProvider from within a controller. I have a custom membership implementation that has a custom User property that describes the logged in user. At the heart of the issue is that I need to retrieve this User object.
My application has done a skeletal implementation of the MembershipProvider, but it works fine and correctly validates users that attempt to login or create a new account. Because of that, I have kept the default implementation of the AccountController that comes out of the box for an ASP .NET MVC application.
If you need anymore details, I would be happy to provide them.
If you create a new ASP.NET MVC project in Visual Studio, it will automatically create a controller called
AccountControllerfor you. This controller provides an example of how you are supposed to access the MembershipProvider in ASP.NET MVC.In short, it promotes Dependency Injection (DI) and loose coupling by hiding the MembershipProvider behind the
IMembershipServiceinterface.The class
AccountMembershipServiceimplementsIMembershipServiceas an Adapter ofMembershipProvider.You can inject an instance of
IMembershipServiceinto any controller that needs it, just like theAccountControllerdoes.