Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update custom user profile fields with SimpleMembershipProvider?

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!

like image 591
Mike Perrenoud Avatar asked Nov 05 '12 00:11

Mike Perrenoud


2 Answers

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.

like image 56
Adam Tuliper Avatar answered Nov 17 '22 03:11

Adam Tuliper


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.

like image 40
user2146075 Avatar answered Nov 17 '22 04:11

user2146075