Showing posts with label Profile. Show all posts
Showing posts with label Profile. Show all posts

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!