If you use the VS template to create a new 'internet' web application, then the template includes code for Asp.Net simple membership. But the template is written for SqlExpress. If you instead have a full SQL Server install on your machine it won't work.
The first exception you might see when debugging in visual studio is,
Exception has been thrown by the target of an invocation.
or if you aren't debugging you might see
The system cannot find the file specified
neither of which help at all.
so first, change the connection string in web.config to use your local SQL Server:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20140225162244;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20140225162244.mdf" providerName="System.Data.SqlClient" />
changing the connectionString
's Data Source
property to Data Source=.;
Now you might get the same or a different exception message:
The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
but the linked page doesn't tell you how to fix it.
The inner exception is more helpful:
Directory lookup for the file "c:\...etc...\MvcApplication1\App_Data\aspnet-MvcApplication1-20140225162244.mdf" failed with the operating system error 5(Access is denied.). CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Which tells you that the login account under which SQL Server is running doesn't have write permissions to the directory in which you write you code. It does have write permissions in the SQL Server data directory, for instance C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA
so you'd think that by looking at that directory to see what user it runs under you'd be able to give it permissions.
Almost. The Explorer GUI Security tab showed me a usergroup called 'MSSQLSERVER' but if you try to give permissions to that group you'll find it doesn't exist. More helpful is the command line:
cacls "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL"
which, if you look carefully at the output,
C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL CREATOR OWNER:(OI)(CI)(IO)F NT AUTHORITY\SYSTEM:(OI)(CI)F BUILTIN\Administrators:(OI)(CI)F BUILTIN\Users:(OI)(CI)R NT SERVICE\MSSQLSERVER:(OI)(CI)R
shows you that you the actual name is NT SERVICE\MSSQLSERVER
So I gave modify permissions on my App_Data
directory to NT SERVICE\MSSQLSERVER
(I carefully copy-pasted the full name) and it worked.