Reputation: 2353
At the moment I am working with a MVC4 application and a database. I want to use the default login and register procedure/ methods.
I am using a table with user information like, firstname, surname, password, email...
I just want to use my database with the default register/ login methods. On the internet I read several topics, but none of them give me a correct answer.
This is what I have already did:
1 Use SimpleMembership
So I added:
<add key="enableSimpleMembership" value="true">
2 Added fields to the RegisterModel
3 Change the CreateUserAndAccount parameters too:
WebSecurity.CreateUserAndAccount(model.Email, model.Password, new {FirstName = model.UserName, SurName = model.UserName}, false);
Is this the best way to solve this problem or are there other solutions?
Thank you in advance!
Upvotes: 1
Views: 2309
Reputation: 17540
WebSecurity.CreateUserAndAcccount allows you to tailor how you populate the properties of the UserProfile entity, but you have to update the UserProfile class for it to work out-of-the-box. You can read more about customizing SimpleMembership here. UserProfile is the default entity/table that is used if you just use what was generated by the Internet Template when you created your MVC 4 project. To use another table you will need to change the parameters in WebSecurity.InitializeDatabaseConnection to point to your existing table and define what the user ID column is and the user name column is. The user ID must be an integer so that it can be joined with the webpages_Membership table used by SimpleMembership to store the hashed password an other information required for authentication.
The code generated by the Internet Template uses a lazy loading method of initializing the database with the InitializeSimpleMembershipAttribute, which you will find in the Filters folder of your project. This is where the InitializeDatabaseConnection is called. It is also implemented this way in case the developer does not use forms authentication and the application will still work. If you are using forms authentication I prefer the more straight forward method of database initialization described here.
There are many options with SimpleMembership depending upon your application requirements and I will give you just a few. 1) customize the UserProfile to have the properties you have in your existing database and if you have any existing data migrate it over. 2) Modify InitializeDatabaseConnection and CreateUserAndAccount to work with your existing table. 3) Add a foreign key to UserProfile that will allow you to join a user in SimpleMembership to your existing table.
Upvotes: 2