When we choose New Project --> MVC 4 --> Internet Application
, it will automatically generate AccountController for us. In this controller, I only care about 2 actions, Login
and Register
.
In MVC 3, it uses Membership's static methods, ValidateUser
in Login
action and CreateUser
in Register
. So, if I want to integrate it with my own database, I just need create CustomMembershipProvider
by extending MembershipProvider
and override that two methods.
But in MVC 4, it uses WebSecurity.Login
and WebSecurity.CreateUserAndAccount
. My questions are:
- How can I make it use my own database like I did in MVC 3?
- What's the different? Why doesn't it keep using static Membership methods? Why does it have to change to
WebSecurity
?
Thanks so much for your help.
You will need to implement you own Membership and Role providers.
Custom Provider:
You will just need to override the methods you want to "intercept" (ie ValidateUser). You will also need to register the provider in the web.config file:
You should also have a Filter called "InitializeSimpleMembershipAttribute"
I hope this helps...
ASP.NET MVC 4 uses a new membership provider called SimpleMembershipProvider. WebMatrix is a facade over SimpleMembershipProvider. There is no longer a need to modify the config file with ASP.NET MVC 4. More info here: http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html
1.How can I make it use my own database like I did in MVC 3?
A: No need to override SimpleMembershipProvider if you want to roll your own. Remove the reference to the
InitializeSimpleMembershipAttribute
in theAccountController
class and modify the methods in theAccountController
class.2.What's the different? Why doesn't it keep using static Membership methods? Why does it have to change to WebSecurity?
A: ASP.NET MVC 4 Now uses a new membership provider: SimpleMembershipProvider.