views:

49

answers:

3

The question title says it all. 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.

+2  A: 

I'm guessing your MembershipProvider is registered in web.config. If so, you can use static property System.Web.Security.Membership.Provider.

Mike Chaliy
Thanks for your reply Mike. I've tried this and it will work, but I don't like doing it this way. My reasoning is this: I've created a custom implementation of the MembershipProvider; however, retrieving the provider through this means doesn't retrieve one of the type I've created, but rather MembershipProvider. I could cast this to my type and that works, but I don't know if this is frowned on or not. Furthermore, in situations where the user is remembered, the User property on my custom implementation is null and never gets populated so that is a problem that I am also working on.
Jason
A: 

If you create a new ASP.NET MVC project in Visual Studio, it will automatically create a controller called AccountController for 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 IMembershipService interface.

The class AccountMembershipService implements IMembershipService as an Adapter of MembershipProvider.

You can inject an instance of IMembershipService into any controller that needs it, just like the AccountController does.

Mark Seemann
Thanks for your very prompt reply, Mark. I ask that you bare with me as I'm trying to learn a lot of technologies at once and haven't begun wrapping my head around how to actually use DI, though, I think I understand its purpose. I only partly see what you're saying. Can you elaborate?
Jason
When you install the ASP.NET MVC SDK, you get new project templates in Visual Studio. When you create a new ASP.NET MVC project, the project template will generate a file called AccountController.cs. If you look in this file, you will see sample code that interacts with MembershipProvider.
Mark Seemann
A: 

I agree with Mike that this is the standard way to do this, but to mitigate the confusion of having to cast your Provider each time, you could create a base controller that all of your controllers inherit from and have a protected property with a getter that casts it once. This will accomplish better code re-use and if you ever change your membership provider you'll only have to do it in once place.

Matt Wear