Sunday, 8 December 2013

How to implement Simple Membership in MVC 4

Importanat Note: We can initialize simple Membership in MVC 4 by two ways. 

 1.   By InitializeSimpleMembershipAttribute which is already present on Account Controller.
 2.   FromGlobal.asax.

Here we are implementing it with second way. 

So First of all remove InitializeSimpleMembershipAttribute  from Account Controller and follow these steps.

 1. Copy this function in Global.asax 

  void initializeMembership()
            if (!WebSecurity.Initialized)
                WebSecurity.InitializeDatabaseConnection("ConnectionString""UserProfile""UserId""UserName", autoCreateTables: true);


2. This function checks the database of given ConnectionString. Create Membership Table if they are not present, and initializes Simple Membership.

3.    Call this function in the end of Application_Start event. Like given below.

        protected void Application_Start()


4. Tables for membership have now been created in DataBase of given Connection string.

5.  You can also add more fields in UserProfile Table. (Not mentioning here now)

6.  Now add default login url in system.web node in web config if not present.
              <authentication mode="Forms">
                     <forms loginUrl="~/Account/Login" timeout="2880" />

7.   Also add some other settings in system.web in web config. This setting enables SimpleMembershipProvider  explicitly.
    <!--MVC 4 Simple Membership Settings-->
              <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
                           <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
              <membership defaultProvider="SimpleMembershipProvider">
                           <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
    <!--End of MVC 4 Simple Membership Settings-->


No comments:

Post a Comment