public async Task <IActionResult> SignUp(SignUpFormDTO signUpForm) { User registeredUser = await _userManager.FindByEmailAsync(signUpForm.Email); if (registeredUser != null) { return(BadRequest("User is registered.")); } User user = new User { Email = signUpForm.Email, UserName = signUpForm.UserName }; await _userManager.AddToRoleAsync(user, "User"); var result = await _userManager.CreateAsync(user, signUpForm.Password); if (result.Succeeded) { _serviceWrapper.Save(); await _signInManager.SignInAsync(user, false); return(Ok("Account created")); } else { return(BadRequest("Incorrect login or password")); } }
public async Task <IActionResult> SignUp(SignUpFormDTO signUpForm) { User registeredUser = await _userManager.FindByNameAsync(signUpForm.UserName); if (registeredUser != null) { return(Ok(false)); } User user = new User { Email = signUpForm.Email, UserName = signUpForm.UserName }; var result = await _userManager.CreateAsync(user, signUpForm.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "User"); _serviceWrapper.Save(); await _signInManager.SignInAsync(user, false); var roles = await _userManager.GetRolesAsync(user); return(Ok(new { user.UserName, user.Email, Role = roles.First() })); } else { return(Ok(false)); } }
public async Task <ActionResult> ChangRole(string id, string roleName) { var role = await _roleManager.FindByNameAsync(roleName); if (role == null) { return(NotFound(roleName)); } User user = await _userManager.FindByIdAsync(id); if (user != null) { if (await _userManager.IsInRoleAsync(user, roleName)) { return(Ok(id)); } var userRoles = await _userManager.GetRolesAsync(user); userRoles.Remove(roleName); await _userManager.RemoveFromRolesAsync(user, userRoles); await _userManager.AddToRoleAsync(user, roleName); _serviceWrapper.Save(); return(Ok(id)); } return(NotFound(id)); }