AspNetCore Identity
While logged in, I want the the ability to create new users. I am using the following code, basically resembles the same code when a user registers with the site.
The user is being created in the database and the proper email is sent for email confirmation. However, if I try and login with the new user, it fails. I can create a new user using the registration form and everything works fine. The only difference I see is that during the registration process I am not a logged in user.
Can someone shed some light on this issue for me? Thanks!
var user = new ApplicationUser { UserName = userProfileDto.Username, Email = userProfileDto.Email, FirstName = userProfileDto.FirstName, LastName = userProfileDto.LastName, Address = userProfileDto.Address, PhoneNumber = userProfileDto.CellPhone, City = userProfileDto.City, State = userProfileDto.State, ZipCode = userProfileDto.ZipCode, Organization = userProfileDto.Organization };
var result = await _userManager.CreateAsync(user, userProfileDto.Password);
if (result.Succeeded)
{ // Send an email with this link
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(userProfileDto.Email, "Confirm your account",$"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
_logger.LogInformation(3, "User was created with a new account with password.");
userProfileDto.Id = user.Id;
return RedirectToAction("Edit", new { userId = user.Id });
}