Alex Rivera | Logout

How do I manage profiles using SimpleMembership?

Asked 2012-09-10T12:56:47.817
19

I have an ASP.NET MVC 4 site based off the internet template. I am using the SimpleMembership which i set up with that template.

I can modify the Users table which has been creted for me but I am unsure as to the "correct" way to modify the extra fields I have added. I want Fullname, Email etc and have added them to the user table but there appears no way to update through the SimpleMembership WebSecurity.* static methods.

Are you supposed to just update those properties yourself using EF outside of the SimpleMembership API?

Edit
Report

1 Answer

15

They made it easy to modify the profile with SimpleMembership. SimpleMembership is using the code first EF model and the user profile is defined in the file AccountModels.cs that is generated as part of the Internet template for MVC 4. Just modify the class UserProfile and add the new fields in the class definition. For example, adding a field for email would look something like this:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
}

Here is an example on how you would access the email field:

var context = new UsersContext();
var username = User.Identity.Name;
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
var email = user.Email;

Here is what the database looks like after adding the email field.

enter image description here

There is a good blog that describes some of the changes in SimpleMembership here. You can also find more detailed information on customizing and seeding SimpleMembership here.

answered 2012-09-10T14:39:38.427

Your Answer