7

I added a custom field to the UserProfile table named ClassOfYear and I'm able to get the data into the profile during registration like this:

var confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName,
    model.Password,
    propertyValues: new { ClassOfYear = model.ClassOfYear },
    requireConfirmationToken: true);

However, now I want to be able to update the profile when I manage it but I can't seem to find a method to do so. Do I need to simply update the UserProfile table myself? If not, what is the appropriate way of doing this?

FYI, I'm using Dapper as my data access layer, just in case it matters. But, like stated, I can just update the UserProfile table via Dapper if that's what I'm supposed to do, I just figured that the WebSecurity class, or something similar, had a way already since the custom user profile fields are integrated with the CreateUserAndAccount method.

Thanks all!

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232

2 Answers2

4

There is nothing in the SimpleMembershipProvider code that does anything with additional fields except upon create.

Simply query the values yourself from your ORM.

You can use the WebSecurity.GetUserId(User.Identity.Name) to get the user's id and then Dapper to query the UserProfile table.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • 1
    Awesome, thanks! The `WebSecurity.GetUserId` method just got rid of a TON of sub queries I had written too! Thanks a lot! – Mike Perrenoud Nov 12 '12 at 17:09
  • 1
    Arg. I think I was wrong here, been diving more into the details as its mostly undocumented. After more research I think there is a way to do this now : ) See: WebSecurity.CreateUserAndAccount(userName, password,new { CustomPropertyName= WhateverValue },false); You can download this source file and see it takes a dictionary and parses each value and inserts into the database. – Adam Tuliper Nov 15 '12 at 06:23
  • But that won't perform an `UPDATE` right? Because I'm using that method to `INSERT` the values already (as you can see in my question in fact). – Mike Perrenoud Nov 15 '12 at 12:59
  • 2
    Ha, ya. See what happens with a few days of very little sleep? : ) Cheers ;) – Adam Tuliper Nov 15 '12 at 14:34
  • Cheers friend, and take care! – Mike Perrenoud Nov 15 '12 at 18:52
2

Just in case anyone facing the same problem. After fighting a lot with the SimpleMembership I got a solution that populates both the webpages_Membership and my custom Users table. For clarification follow my code:

public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            TUsuario userDTO= new TUSer()
                {
                    Name = model.Name,
                    Login = model.Login,
                    Pass = model.Pass.ToString(CultureInfo.InvariantCulture),
                    Active = true,
                    IdCompany = model.IdCompany,
                    IdUserGroup = model.IdUserGroup,
                };
            try
            {
                WebSecurity.CreateUserAndAccount(model.Login, model.Pass, new { IdUser = new UserDAL().Seq.NextVal(), Name = userDTO.Name, Login = userDTO.Login, Active = userDTO.Active, Pass = userDTO.Pass, IdCompany = userDTO.IdCompany, IdUserGroup = userDTO.IdUserGroup });

                WebSecurity.Login(model.Login, model.Pass);

After cursing the framework a lot, that gave me a bliss of fresh air :)

PS.: The users table is specified in the global.asax file using the WebSecurity.InitializeDatabaseConnection functon.