Reputation: 11
Here is my problem. I am creating an MVC 4 EF site from a database that I have created myself. I have a USERS
table with a UserID
column, a UserName
column, a Hash
column, and a Salt
column. I would like to use simple membership and point it to my USERS
table to handle logins, password changes, registrations etc. I am using the MVC 4 Web Application Template and cannot get it to connect to my database. Here is my connection string for the InitializeSimpleMembershipAttribute
class to use. I was told it needed to be different from the connection string that created my entities from the database.
<add name="DBConnectionString"
connectionString="data source=SERVERADDRESS;initial catalog=DATABASENAME;user id=USERID;password=PASSWORD;"
providerName="System.Data.SqlClient" />
The only changes I made to the template's InitializeSimpleMembershipAttribute.cs
is that I replaced the following line with my table data:
WebSecurity.InitializeDatabaseConnection("DBConnectionString", "USERS", "UserID", "UserName", autoCreateTables: true);
I am not sure what I am doing wrong. I have read that all you need do is give the database connection the connection string, the table for the users, the user ID column, and the username column. Does my user account database need to be exactly like the template's? This is the first time I have done database first for an MVC site.
Upvotes: 1
Views: 1384
Reputation: 41
1.) Make sure the following lines in your web.config file.
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
2.) Add the following codes to Application_Start() event in your global.asax.cs file.
Database.SetInitializer<UsersContext>(null);
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DBConnectionString", "Users", "UserId", "UserName", autoCreateTables: true);
}
3.) and then Clean and Run solution...
Upvotes: 2