7

I would like to add user accounts to my MVC 3 web app.For each user account I would like to add a corresponding user profile, with the usual email and address contact info, etc. Also maybe add some security questions for when the user forgets there login credentials.

I’ve read some info about using the provided ASP.net profile options and also some people suggest using your own membership tables as it quicker retrieveing user profile info especially when the user list grows.

Can anyone give me some insight on which approach to use and also point me in the direction of a any tutorials showing me how to implement User accounts and profiles within an MVC 3 Web Application?

Many thanks D

davey
  • 1,544
  • 4
  • 26
  • 41

2 Answers2

5

If you aren't going to be querying these tables outside of what is built in for profiles, stick with the default out of the box roles/profile. Its a bit of additional work, troubleshooting, and development for custom profiles. Its not that its hard.. its just additional work. If you don't have a specific need to it use what is built in. As for performance, look at what you'll be running on. Do you have to squeeze out every bit of performance from the hardware you are running on? Most places do not, and have multiple sites on servers barely touching the disk and cpu, in that case (as is fairly typical) you are fine with everything built in.

To implement the accounts, create a new mvc3 project and in visual studio in the solution explorer click on the last icon on the right up top for "asp.net configuration" and manage

There are however many tutorials out there on implementing custom table providers in asp.net and they DO apply just as much to mvc since mvc is just an asp.net app.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Hi Adam, I will probably want to customize my profiles for example adding a User's picture URL and also use Profile data such as Username and User picture in other areas of the site. Do have a link to a custom profile tutorial? Thanks – davey May 04 '11 at 03:51
  • 1
    See: http://stackoverflow.com/questions/834166/implementing-custom-profile-provider-in-asp-net-mvc and http://johnnycoder.com/blog/2010/04/01/asp-net-mvc-custom-profile-provider/ – Adam Tuliper May 04 '11 at 04:15
  • Thanks for the link Adam. I've already had a quick look over that post but I’m looking for more a step by step Tutorial with a bit more explanation. Also the poster isn’t using EF he instantiates a SQL conn and calls a sproc. – davey May 04 '11 at 05:01
  • I'd have to google for some step by steps - maybe I'll put something together on this. em@il me at adam.tuliper at g mail dotcom as for EF vs. Sql server, simply replace in GetPropertyValues the sql connection code with using(var ctx = new EntityDbcontext()) { var profile = (from o in ctx.Profile where o.UserId = context["UserId] select o).First(); ...load properties here } – Adam Tuliper May 05 '11 at 16:51
5

ProfileBase is the class you should look at.

Some sample code copied from one of my web site:

var profile = ProfileBase.Create(model.UserName);
profile.SetPropertyValue("Identity.FirstName",model.FirstName);
profile.SetPropertyValue("Identity.LastName", model.LastName);
profile.SetPropertyValue("Identity.FullName", model.FullName);
profile.SetPropertyValue("Contact.Email", model.Email);
profile.SetPropertyValue("Contact.Phone", model.PhoneNumber);
profile.Save(); 

Hope it is useful to you.

Blaise
  • 21,314
  • 28
  • 108
  • 169