ASP.NET MVC Forms Authentication with SqlMembershipProvider

The ASP.NET MVC Website Template comes pre-configured to use SqlMembershipProvider which manages storage of membership information for an ASP.NET application in a SQL Server database.

But before you can use this properly you will need to setup your database, which will be very easy because Microsoft shipped .NET 2.0 with a nice utility to do it! You can find this on your computer, at this path C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe

1

Your database should have these tables:

2

Now lets tell our ASP.NET MVC Website’s configuration where to find this database. Open the web.config and change the ApplicationServices connection string to point to your SQL Server. You should now rebuild your solution to avoid any problems in the next step!

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=localhost;
                           Initial Catalog=database;
                           Persist Security Info=True;
                           User ID=user;
                           Password=password"
         providerName="System.Data.SqlClient"
    />
</connectionStrings>

It’s all good and nice but now we need Users in this database otherwise it’s completly useless! So open up your ASP.NET MVC Solution in Visual Studio and go to Project » ASP.NET Configuration. This will open the ASP.NET Web Application Administration website. In the Security tab you can Add/Manage Users and Roles. For the moment, add a user so we can try the login. I’ve added user Mike.

3

You can now run your application and click on [ Log On ] at the top right corner of the page, this will redirect you the the logon form.

4

If you entered the correct credential you should be redirected to the home page of the site and you will see your name and a Log Off button instead of the Log On button.

5

Well congraticulations! You now have SqlMembershipProvider working on your website, wasn’t so hard, was it? Not a single line of code required! I’ll be posting again soon to show you how to use Roles in your applications.

This entry was posted on Wednesday, April 22nd, 2009 at 9:16 pm and is filed under ASP.NET, C#.NET, Developement, MVC, SQL Server. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

10 Responses to “ASP.NET MVC Forms Authentication with SqlMembershipProvider”

kk October 9th, 2009 at 1:15 am

Very good article. Do you have the post for using Roles?

Amadiere January 10th, 2010 at 5:58 pm

Thanks, nice screenshots and a useful guide. My brain has been starved of coffee and this was just what I needed to kick start me going again! :)

Josh March 27th, 2010 at 3:38 pm

Thanks, very helpful!

Josipi December 27th, 2010 at 11:09 pm

Thanks, as far as you go. How about the final step(s) for requiring login before a page can be accessed?

Drew Freyling December 29th, 2010 at 5:09 am

Good to see it doesn’t work for sql 2008 databases when you try to select a database :(

Nchekwube March 11th, 2011 at 9:28 am

Nice and clear. How about if you want to add extra information on the users? And not use the profile provider because it stores the information inefficiently?…

OpenFaq : Business Requirements in Code (Part 2) « .NET Unplugged March 28th, 2011 at 3:38 am

[...] When I ran acceptance tests I received errors having to do with MembershipProvider not being setup. To set it up on my local Sql Server DB I used steps here: http://helios.ca/2009/04/22/aspnet-mvc-sqlmembershipprovider/ [...]

OpenFaq (Part 2): Business Requirements in Code « .NET Unplugged April 5th, 2011 at 2:20 am

[...] When I ran acceptance tests I received errors having to do with MembershipProvider not being setup. To set it up on my local Sql Server DB I used steps here: http://helios.ca/2009/04/22/aspnet-mvc-sqlmembershipprovider/ [...]

ASP.NET , PHP , WEB DESIGN | Размисли July 13th, 2011 at 5:00 am

[...] http://helios.ca/2009/04/22/aspnet-mvc-sqlmembershipprovider/ – ASP.NET MVC Forms Authentication With SqlMembershipProvider http://subtextproject.com – Subtext is a blog engine, C#, BSD license http://code.google.com/p/subtext/ – Subtext code http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all – CodeSnip: Handle Browser Close Event on the Server-Side [...]

Adidas Shoes Price List September 20th, 2012 at 2:27 am

Hοwdу! Dо you use Τwitter?
I’d like to follow you if that would be ok. I’m unԁoubtеdlу enjoying your blog and look forωard tο new updateѕ.

Leave a Reply