Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC User Profiles

I've done MVC in the past, but I am new to ASP and ASP MVC. I really love the ease that ASP MVC provides me, so far, but I am having trouble figuring out how to get more control over the Users. By default, the MVC provides a minimal user registration form. I have looked around quite a bit, but I still have two questions:

  1. How do I make the User data base a local database in my project? I think SQLEXPRESS is used to store the user values, in what seems like a magical process. How do I de-magic-ify this? I would like to have more control on the location of this database.

  2. This leads to another question: How do I expand the User? I have been reading up on Profiles, but I am still confused about a few things. How do I prepare a Profile and link it with a User? What serves as the foreign key? And, in my controllers, how can I access various parts of the user like username, email, or even from the profile stuff like firstname, lastname (though I guess once when I have a Profile's database and a User's database locally, I can run sql commands to retrieve data)

I would really appreciate some pointers to the right resources, and/or best practices with ASP.NET

like image 388
mvcn00b Avatar asked Nov 09 '10 20:11

mvcn00b


2 Answers

I would start by reading this official Microsoft article on extending the ASP.NET Membership API. It talks about creating extra tables for storing additional information about users.

The membership database

  1. If you have an existing database which holds all your other website information, you can run the aspnet_regsql.exe tool to generate the necessary user tables. You will then need to modify your web.config and add the SqlMembershipProvider along with your connection string.
  2. If you're creating a new project and don't have a database, start with a new MVC project which already has Membership enabled. Your database will be created inside the App_Data folder on first use, and you can take this and attach it to your SQL/SQLEXPRESS server. Then it's just a matter of changing the connection string to use a DB server rather than a local file.

Creating additional tables

This part is actually quite simple and consists of a few short steps:

  1. Create a new table, i.e. UserProfiles
  2. Add a uniqueidentifier column as your primary key, and add a foreign key to the aspnet_Users table
  3. Add any other fields you want to store (Phone, Address, Gender etc.)
  4. If you're using LINQ-to-SQL or the Entity Framework, you can simply drag the tables you need onto the designer, and you'll be ready to query the Membership tables.

Here's a little sample on usage

Add this snippet to your repository responsible for Profile/Account information.

public aspnet_User GetUser()
{
    MembershipUser user = Membership.GetUser();
    return db.aspnet_Users.SingleOrDefault(u => u.UserId == user.ProviderUserKey);
}

Then inside your models, you can get the user and access the other information stored in your UserProfiles table.

AccountRepo accountRepo = new AccountRepo();
aspnet_User user = accountRepo.GetUser();
string Address = user.UserProfile.Address; // bingo!

And that's pretty much it!

This is obviously a simple example, and you should be checking if the user is null and you could also create a class responsible for returning the necessary information about a user, implement caching, etc..

like image 117
Marko Avatar answered Oct 09 '22 11:10

Marko


I would start from here:

  • Managing Users by Using Membership
  • Managing Authorization Using Roles

Also a great article series (18 articles!!!) is from Scott Mitchell at 4GuysFromRolla.

The ASP.NET membership model is desgned to have a pluggable architecture. You can write you own MembershipProvider implementation that best suit your needs.

Even if most of the samples you will find on the net regards ASP.NET web forms, there are only very small differences when used with MVC.

like image 29
Lorenzo Avatar answered Oct 09 '22 11:10

Lorenzo