public async Task <IActionResult> AddUser([FromForm] NewAppUser newAppUser) { var appUser = new AppUser { UserName = newAppUser.UserName, Email = newAppUser.Email }; await _userManager.CreateAsync(appUser, newAppUser.Password); return(RedirectToAction("Login", "Account")); }
public async Task <IActionResult> Create([FromBody] NewAppUser postUser) { // Check simplistic username and password validation rules bool isValid = IsValidUserAndPasswordCombination(postUser.Username, postUser.Password); if (isValid) { // Does the user already exist? AppUser user = _context.AppUser.SingleOrDefault(u => u.UserName == postUser.Username); if (user != null) { // Found the user, verify credentials var result = await _signInManager.PasswordSignInAsync(postUser.Username, postUser.Password, false, lockoutOnFailure : false); // Password is correct, generate token and return it if (result.Succeeded) { return(new ObjectResult(GenerateToken(user.UserName))); } } else { var userstore = new UserStore <AppUser>(_context); // User does not exist, create one user = new AppUser { Name = "Generic", UserName = postUser.Username, NormalizedUserName = postUser.Username.ToUpper(), Email = postUser.Username, NormalizedEmail = postUser.Username.ToUpper(), EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString("D") }; var passwordHash = new PasswordHasher <AppUser>(); user.PasswordHash = passwordHash.HashPassword(user, postUser.Password); await userstore.CreateAsync(user); // await userstore.AddToRoleAsync(user); _context.SaveChanges(); return(new ObjectResult(GenerateToken(user.UserName))); } } return(BadRequest()); }