public async Task<IHttpActionResult> Register(UserModel model) { try { if (!ModelState.IsValid) return BadRequest(ModelState); using (AuthRepository repo = new AuthRepository()) { IdentityResult result = await repo.RegisterUser(model); if (!result.Succeeded) { foreach (string error in result.Errors) ModelState.AddModelError(string.Empty, error); return BadRequest(ModelState); } } } catch (Exception ex) { ErrorLogging.LogError(ex); return InternalServerError(ex); } return Ok(); }
public async Task<IdentityResult> RegisterUser(UserModel userModel) { ApplicationUser user = new ApplicationUser(); try { user = new ApplicationUser { UserName = userModel.Email, Email = userModel.Email, TwoFactorEnabled = true//, //PhoneNumber = userModel.MobilePhoneNumber }; IdentityResult result = await UserManager.CreateAsync(user, userModel.Password); if (result.Succeeded) { var roleresult = UserManager.AddToRole(user.Id, RoleType.NonClient); //if (string.IsNullOrEmpty(userModel.SSN)) // userModel.SSN = null; using (WW_DevEntities context = new WW_DevEntities()) { Client model = new Client { Id = Guid.NewGuid(), AspUserId = user.Id, FirstName = ProperCase.Convert(userModel.FirstName), LastName = ProperCase.Convert(userModel.LastName), MiddleName = ProperCase.Convert(userModel.MiddleName), Suffix = ProperCase.Convert(userModel.Suffix), //Gender = userModel.Gender, //SSN = userModel.SSN, //DateOfBirth = userModel.DateOfBirth, CreatedDate = DateTime.Now }; context.Clients.Add(model); context.PasswordHistories.Add(new PasswordHistory { CreatedDate = DateTime.Now, HashPassword = Hash.Get(userModel.Password), UserID = user.Id }); //ClientPhone mPhone = new ClientPhone(); //ClientPhone wPhone = new ClientPhone(); //ClientPhone hPhone = new ClientPhone(); //if (userModel.MobilePhoneNumber != null) //{ // mPhone.PhoneNumber = userModel.MobilePhoneNumber; // mPhone.CreatedDate = DateTime.Now; // context.ClientPhones.Add(mPhone); //} //if (userModel.WorkPhoneNumber != null) //{ // wPhone.PhoneNumber = userModel.WorkPhoneNumber; // wPhone.CreatedDate = DateTime.Now; // context.ClientPhones.Add(wPhone); //} //if (userModel.HomePhoneNumber != null) //{ // hPhone.PhoneNumber = userModel.HomePhoneNumber; // hPhone.CreatedDate = DateTime.Now; // context.ClientPhones.Add(hPhone); //} await context.SaveChangesAsync(); //if (userModel.MobilePhoneNumber != null) //{ // context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative() // { // Id = model.Id, // ClientPhoneId = mPhone.Id, // PhoneTypeId = Guid.Parse(Config.MobilePhoneType), // CreatedDate = DateTime.Now // }); //} //if (userModel.WorkPhoneNumber != null) //{ // context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative() // { // Id = model.Id, // ClientPhoneId = wPhone.Id, // PhoneTypeId = Guid.Parse(Config.WorkPhoneType), // CreatedDate = DateTime.Now // }); //} //if (userModel.HomePhoneNumber != null) //{ // context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative() // { // Id = model.Id, // ClientPhoneId = hPhone.Id, // PhoneTypeId = Guid.Parse(Config.HomePhoneType), // CreatedDate = DateTime.Now // }); //} //await context.SaveChangesAsync(); await SendConfirmationEmail(user.Id, EmailMessage.NewAccountEmailConfirmation); } } return result; } catch (Exception ex) { //if error on creating address and phone need to remove login UserManager.Delete(user); ErroLogging.LogError(ex); throw ex; } }