Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 25, 2013 01:57 AM by AppDevForMe
Jan 23, 2013 11:52 PM|LINK
I am using MVC 4 for a new web project. I need to implement forms authentication. Now, I noticed that when I create a new project, that some authorization/authentication code is created for me. Both in the controller and view code.
However, this code appears as if it is targeted to a WebMatrix default .sdf file, which is a sql file. That is nice, however I need to authenticate against an existing database table, in our existing database.
What do I have to do, in order to modify the code to point at a table which already exists for our companies users?
I actually pointed the connection code at our database, however when the existing code is excecuted, and exception will occur. For example below I call the WebSecurity.CreateAccount method, and it will fail.
var token = WebSecurity.CreateAccount("firstname.lastname@example.org", "Sunday", false);
Jan 24, 2013 12:27 AM|LINK
It depends on how your database was set up for authentication. Did you use aspnet_regsql to generate the tables that the default membership provider needs? http://msdn.microsoft.com/en-us/library/ms229862(v=vs.80).aspx
If you did, then you should just be able to change the connetion string to point to that database. I used to have a authentication database shared between multiple apps and just changing the connection string did it for me.
Now if it's a custom schema that your company has defined you would need to implement a custom membership provider
Jan 24, 2013 12:38 AM|LINK
MVC 4 uses SimpleMembership Provider and there is an attribute - \Filters\InitializeSimpleMembershipAttribute.cs which is responsible for initializing the database for the application, it internally calls
WebSecurity.InitializeDatabaseConnection method, this method needs the connection string
name, User Table name (you can use your existing table), UserId column - (must be int type) and UserName column name.
If you are able to map your table with InitializeDatabaseConnection parameter it should work for you.
Jan 25, 2013 01:57 AM|LINK
Thanks CPrakash82, works great!