Monday, April 30, 2012

Membership in ASP.NET MVC 4 (System.Web.Providers)

ASP.NET MVC 4 came with many changes from MVC 3, and one of them was the way membership was implemented.
Membership and profiles tables in MVC 4 are reduced to a few, and they appear to be quite clean. Names like "Memberships, Profiles, Applications, Roles, etc" are for quick to identify their purpose.
Also their structure is quite simplified.

ASP.NET MVC 4 membership uses Entity Framework as its ORM.

When you create a new MVC 4 website project (as Internet application), it gets created with controller, models, and views for Logon, Registration, and all other membership functionality. By default, connectionstring is set to a local SQL Express database (as it used to be in earlier versions).

If you create an empty MVC 4 website project, you will not be able to see any of these (as you only opted it!), but still you can use MVC 4's membership features by using "System.Web.Providers" library.
Make sure, its added as a reference in your solution. (it is there in most cases).
But If not, you can add it using NuGet package manager.
For that, Go to following command in Visual Studio menu.
Tools >> Library Package Manager >> Package Manager Console
This will open up command shell at the bottom. Type in following command and press enter, and this will do all necessary changes in your project and configurations

install-package "System.Web.Providers"

Once done, go to web.config, and configure a connectionstring named "DefaultConnection" to use your remote connection (instead of default SQL Express database).
But make sure your connectionstring includes "MultipleActiveResultSets=True" configuration, otherwise it will give you an error related to "Entity framework" while performing any membership operations.

Sample DataSource settings in connectionstring:
Data Source=myPC;Initial Catalog=myTestDB;Persist Security Info=True;User ID=nirman;Password=nothing*001;MultipleActiveResultSets=True;

Once done, your application is set for membership.
And as you run your MVC application for the first time, and attempts to register a new user, it automatically will create membership tables in SQL Server database. ASP.NET MVC 4 does not use Stored Procs, etc in background for Membership, as it has used EF as its ORM. So you will see only tables got created in database. Less complicated and easily manageable, isn't it!

10 comments:

  1. Sorry but for me, SQl server connection, the database is not created automatically so SQl error on AccountController when the user is going to be created

    ReplyDelete
    Replies
    1. You need to create database manually.
      Create an empty database, and give a valid connectionstring pointing to that database in your web.config file.
      Once done, try running application, and your MVC application will create Membership tables automatically in that database as soon as you register first user there.
      Post if you still face any issue.
      Thanks

      Delete
    2. how do i enable role based authorization for my controller

      Delete
  2. Hi, I tried this, but I got a following error message "To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider"." in this method:

    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

    Can you help me what did I wrong?

    ReplyDelete
  3. typically, this error occurs if you do not initialize membership before calling relevant methods.. If you are using a controller (like AccountController) where this method is called, simply decorate your controller by "[InitializeSimpleMembership]" attribute.

    example:
    [InitializeSimpleMembership]
    public class AccountController : Controller
    {
    ....
    }

    ReplyDelete
  4. Hello, I Have the same problem as Laszlo Uracs ... The [InitializeSimpleMembership] is at AccountController but still the same problem.. Please can you help me ? Thanks for good topic !

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  5. Thanks for the great article! However, what do you do if you already have the Membership tables in your database with data in them and want to upgrade? Some of the data types in the old version are obsolete (ntext), and many of the tables may not be used from the sounds of it. I'm just wondering if there is some kind of conversion out there. Thanks!

    ReplyDelete
  6. Thank you for detailed article, saved me a couple of days of work :-)

    ReplyDelete

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman

Blog Archive