Question

I´ve added some custom attributes to UserProfile asp.net membership table, lastname, address, cellphone, etc. So I can create new users using.

WebSecurity.CreateUserAndAccount(UserName, Password,
        propertyValues: new
        {
          UserId = model.userId,
          UserLastName = model.lastName,
          UserAddress = model.address,                                                
          .
          .
         }
);

So I want to know if it´s possible in similar way achieve an update query, including this custom attributes. Thank you.

Was it helpful?

Solution

Yes you can. However I do not think WebSecurity provides a way to update extra column in membership tables e.g. UserProfile through its API.

This is how we did it, we have MVC4 ASP.NET internet application project and we are using EF5.0 Code First.

You already know how to add extra column in the UserProfile table (table name can be anything).

Once we have a class (that has all required extra columns along with UserId and UserName),

  1. Added a controller UserController dedicated to facilitate CRUD operation for UserProfile.
  2. UserController uses a UserService class in business layer that is taking care of all CRUD operation on UserProfile class (entity).
  3. On Edit Post operation, controller call UserService class UpdateUser() method which looks like below:

    public void UpdateUser(UserProfile user)
    {
        Guard.ArgumentNotNull(user, "user");
    
        if (_roleWrapper.GetRolesForUser(user.UserName).Any())
        {
            _roleWrapper.RemoveUserFromRoles(user.UserName, _roleWrapper.GetRolesForUser(user.UserName));
        }
    
        if (!_roleWrapper.IsUserInRole(user.UserName, user.Role))
        {
            _roleWrapper.AddUserToRole(user.UserName, user.Role);
        }
    
        _dataContext.Update<UserProfile>(user);
    }
    

Above text and sample is for example only, you can simplify it. Basically you need to get hold of UserProfile class and using DbContext to update it manually. WebSecurity has limited APIs keeping simplicity in mind.

Hope that helps (if anything confusing please let me know, i will expand further).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top