I'm new to ASP.NET and have just starting to learn ASP.NET MVC 3. I've started a new ASP.NET MVC 3 project using the default template, which already has most of the membership stuff already configured. Now I'm trying to understand what is really going on behind the scenes.
I've found that default template defines an IMembershipService interface which is implemented by the AccountMembershipService class which basically just wraps a System.Web.Security.MembershipProvider. From the comments in the code is sounds like this abstraction is done to facilitate unit testing.
The default MembershipProvider is set in the Web.config file. My project, which was created from the default template, it is set to the SqlMembershipProvider.
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
This references a connection string in the Web.config file, which references a SQL Express database "aspnetdb.mdf".
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
When I first created my project there was no aspnet.mdf file. But after running my app and registering a new user this file was automatically generated tables and all. What's going on here? What creates this file and specified the tables that should be created?
These are created by the SqlMembershipProvider class. If you'd like, you can customize your config file to use a different membership provider or to tell it what existing database to use.
Use this tool to add the appropriate tables, etc to your existing SQL Server installation: http://msdn.microsoft.com/en-us/library/ms229862(v=VS.100).aspx