public IHttpActionResult PostNewUser(UserCreationModel newUser) { if (!newUser.Password.Equals(newUser.Repeat, StringComparison.InvariantCulture)) { return(BadRequest("Passwords do not match")); } var user = new List9User { Name = newUser.Name, UserName = newUser.UserName, Email = newUser.Email, PhoneNumber = newUser.PhoneNumber, Type = newUser.Type, }; var result = UserManager.Create(user, newUser.Password); if (result.Succeeded) { foreach (var role in newUser.Roles) { UserManager.AddToRole(user.Id, role); } if (!string.IsNullOrWhiteSpace(newUser.Email)) { //Validate Email } if (!string.IsNullOrWhiteSpace(newUser.PhoneNumber)) { //Validate Email } return(Ok(new { Name = user.Name, Type = user.Type, Email = user.Email, PhoneNumber = user.PhoneNumber, UserName = user.UserName, Roles = newUser.Roles, LockedOut = user.LockoutEnabled, EmailConfirmed = user.EmailConfirmed, PhoneNumberConfirmed = user.PhoneNumberConfirmed })); } else { return(BadRequest(String.Join(", ", result.Errors))); } }
public IHttpActionResult PutUser(UserCreationModel newUser) { if (!newUser.Password.Equals(newUser.Repeat, StringComparison.InvariantCulture)) { return(BadRequest("Passwords do not match")); } var user = UserManager.FindByName(newUser.UserName); bool emailChanged = false, phoneChanged = false; if (!user.Email.Equals(newUser.Email, StringComparison.InvariantCultureIgnoreCase)) { emailChanged = true; user.EmailConfirmed = false; } if (!user.PhoneNumber.Equals(newUser.PhoneNumber, StringComparison.InvariantCultureIgnoreCase)) { phoneChanged = true; user.PhoneNumberConfirmed = false; } user.Name = newUser.Name; user.UserName = newUser.UserName; user.Email = newUser.Email; user.PhoneNumber = newUser.PhoneNumber; user.Type = newUser.Type; var result = UserManager.Update(user); if (result.Succeeded) { var roles = UserManager.GetRoles(user.Id); foreach (var role in roles) { if (!newUser.Roles.Contains(role)) { UserManager.RemoveFromRole(user.Id, role); } } foreach (var role in newUser.Roles) { if (!roles.Contains(role)) { UserManager.AddToRole(user.Id, role); } } if (emailChanged && !string.IsNullOrWhiteSpace(user.Email)) { //TODO: validate Email } if (phoneChanged && !string.IsNullOrWhiteSpace(user.PhoneNumber)) { //TODO: validate Email } return(Ok(new { Name = user.Name, Type = user.Type, Email = user.Email, PhoneNumber = user.PhoneNumber, UserName = user.UserName, Roles = newUser.Roles, LockedOut = user.LockoutEnabled, EmailConfirmed = user.EmailConfirmed, PhoneNumberConfirmed = user.PhoneNumberConfirmed })); } else { return(BadRequest("Errors Present")); } }