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.
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.
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.
AppDevForMe
Participant
1396 Points
1327 Posts
Using the generated MVC 4 code for authorization/authentication against my custom database.
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("skilder@hotmailer.com", "Sunday", false);
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Using the generated MVC 4 code for authorization/authentication against my custom database.
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
http://msdn.microsoft.com/en-us/library/f1kyba5e(v=vs.100).aspx
Blog | Twitter : @Hattan
CPrakash82
All-Star
18284 Points
2841 Posts
Re: Using the generated MVC 4 code for authorization/authentication against my custom database.
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.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
If you are able to map your table with InitializeDatabaseConnection parameter it should work for you.
AppDevForMe
Participant
1396 Points
1327 Posts
Re: Using the generated MVC 4 code for authorization/authentication against my custom database.
Jan 25, 2013 01:57 AM|LINK
Thanks CPrakash82, works great!