Month: August 2013

Installing Default membership database in your custom database in MVC 4

 

Hi, In the below post we are going to learn how to install the default membership database in your custom database so that you can take full advantage of asp.net default membership provider.

Here are the situation, you have created a database for your MVC project ,now it is time to implement security features in it.

Go to your MVC project solution explorer and under it Filters—>InitializeSimpleMembershipAttribute.cs or vb extension if you are using vb.net for your project.

image

In this file there is a class named “SimpleMembershipInitializer”and its constructor “SimpleMembershipInitializer()” as below .

   1: private class SimpleMembershipInitializer

   2: {

   3:     public SimpleMembershipInitializer()

   4:     {

   5:         Database.SetInitializer(null);

   6:  

   7:         try

   8:         {

   9:             using (var context = new UsersContext())

  10:             {

  11:                 if (!context.Database.Exists())

  12:                 {

  13:                     // Create the SimpleMembership database without Entity Framework migration schema

  14:                     ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();

  15:                 }

  16:             }

  17:  

  18:             WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

  19:         }

  20:         catch (Exception ex)

  21:         {

  22:             throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);

  23:         }

  24:     }

  25: }

Here “UsersContext” is your database context Update all “UsersContext” with your database context class. The database context class is one which you have inherited from “DbContext” .

   1: public class MyProjectDB : DbContext

Now the update code of “SimpleMembershipInitializer” class looks like below.

   1: private class SimpleMembershipInitializer

   2: {

   3:     public SimpleMembershipInitializer()

   4:     {

   5:         Database.SetInitializer(null);

   6:  

   7:         try

   8:         {

   9:             using (var context = new MyProjectDB())

  10:             {

  11:                 if (!context.Database.Exists())

  12:                 {

  13:                     // Create the SimpleMembership database without Entity Framework migration schema

  14:                     ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();

  15:                 }

  16:             }

  17:  

  18:             WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

  19:         }

  20:         catch (Exception ex)

  21:         {

  22:             throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);

  23:         }

  24:     }

  25: }

Now find the below line of code in the same class

   1: WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

and update is as below .

   1: WebSecurity.InitializeDatabaseConnection("MyProjectDBConnectionString", "UserProfileMasters", "UserId", "UserName", autoCreateTables: true);

Here “MyProjectDBConnectionString” is your connection string of the database which you have added on web.config file. “UserProfileMasters” is the name of the table that have user profile information ,”UserId” is the column which contains user Ids. It must be type of int. “UserName” is name of column that have usernames and last “autoCreateTables” is self explanatory , it indicates whether you want the framework to create the membership tables for you or not.

Save and run the application . hit any url of your project which fetch any database information  like list of projects etc.

Now go to your database explorer .

image

and you will find that the framework created the default membership tables for you.

Hope you find this post useful ,Thanks for reading. 🙂