Update custom user profile fields with SimpleMembershipProvider?

ぐ巨炮叔叔 提交于 2019-12-22 04:45:14

问题


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!


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/13224405/update-custom-user-profile-fields-with-simplemembershipprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!