public async Task <IHttpActionResult> Register(RegisterBM m) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = m.Email, Email = m.Email }; var res = await UserManager.CreateAsync(user, m.Password); if (!res.Succeeded) { return(GetErrorResult(res)); } return(Ok()); }
public async Task <IHttpActionResult> Register(RegisterBM model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var appuser = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await this.UserManager.CreateAsync(appuser, model.Password); await this.UserManager.AddToRoleAsync(appuser.Id, "Operator"); if (!result.Succeeded) { return(this.GetErrorResult(result)); } User user = await this.service.Register(appuser, model.Name); user.ApplicationUser = await this.UserManager.FindByIdAsync(user.ApplicationUserId); var roles = await this.UserManager.GetRolesAsync(user.ApplicationUserId); return(this.Ok( new UserInfoVM { Id = user.Id, Name = user.Name, Roles = roles })); }
public async Task <IActionResult> Register([FromBody] RegisterBM registerModel) { // keep track of validation errors to inform client of what went wrong RegisterErrorVM validationErrors = new RegisterErrorVM(); // validate registerModel (automatic through attribute validation): if (!ModelState.IsValid) { // fill in validationErrors object and return it within a 'Bad request' response. validationErrors.InvalidEmail = ModelState.GetValidationState("Email") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; validationErrors.InvalidUsername = ModelState.GetValidationState("Username") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; validationErrors.InvalidPassword = ModelState.GetValidationState("Password") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; validationErrors.InvalidConfirmPassword = ModelState.GetValidationState("ConfirmPassword") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid; return(BadRequest(validationErrors)); } // the registerModel is valid but the email already exists: if (await _userManager.FindByEmailAsync(registerModel.Email) != null) { validationErrors.ExistingEmail = true; return(BadRequest(validationErrors)); } // the registerModel is valid, the email is unique, but username already exists: if (await _userManager.FindByNameAsync(registerModel.Username) != null) { validationErrors.ExistingUsername = true; return(BadRequest(validationErrors)); } // password and confirmPassword, although valid, do not match: if (registerModel.Password.CompareTo(registerModel.ConfirmPassword) != 0) { validationErrors.PasswordsDoNotMatch = true; return(BadRequest(validationErrors)); } // registration data was validated, try to create user: var user = new MyUser { UserName = registerModel.Username, Email = registerModel.Email }; var result = await _userManager.CreateAsync(user, registerModel.Password); if (result.Succeeded) { //user was successfully created //initialize his playlist of private songs: user.Playlists = new List <Playlist>(); Playlist p = new Playlist("Músicas Privadas", user); user.Playlists.Add(p); user.PrivateSongPlaylist = new PrivateSongPlaylist(); user.PrivateSongPlaylist.Playlist = p; await _context.SaveChangesAsync(); //do not automatically sign in, client app will request it upon successful registration //await _signInManager.SignInAsync(user, isPersistent: false); return(Ok(new UserBasicVM(user.UserName))); } else { return(BadRequest()); } }