public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new Technician { Name = Input.Name, UserName = Input.Email, Email = Input.Email, Specialty = Input.Specialty, Level = Input.Level, DOB = Input.Birthday, }; user.AssignRole(_userManager, IdentityData.NonAdminRoleNames[1]); var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
private static void CreateTechnician( string name, DateTime dob, string email, string password, string rol, string level, UserManager <ApplicationUser> userManager) { var user = new Technician { Name = name, DOB = dob, UserName = email, Email = email }; user.Specialty = rol; user.Level = level; user.AssignRole(userManager, IdentityData.NonAdminRoleNames[1]); IdentityResult result = userManager.CreateAsync(user, password).Result; if (result.Succeeded) { IdentityResult addRoleResult = userManager.AddToRoleAsync(user, IdentityData.NonAdminRoleNames[1]).Result; if (!addRoleResult.Succeeded) { throw new InvalidOperationException( $"Unexpected error ocurred adding role '{IdentityData.NonAdminRoleNames[1]}' to user '{name}'."); } } else { throw new InvalidOperationException($"Unexpected error ocurred creating user '{name}'."); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { //excepciones para que el correo sea de la universidad y un rango de la edad try { string[] emailInput = Input.Email.Split("@"); string EmailVerificate = emailInput[1]; Check.Precondition(EmailVerificate == EmailUCU, "El correo debe de ser de la Universidad"); } catch (Check.PreconditionException ex) { return(Redirect("https://localhost:5001/Exception?id=" + ex.Message)); } try { int BirthDateInput = Input.BirthDate.Year; Check.Precondition(BirthDateInput > 1950, "Edad Incorrecta"); Check.Precondition(BirthDateInput < 2010, "Edad incorrecta"); } catch (Check.PreconditionException ex) { return(Redirect("https://localhost:5001/Exception?id=" + ex.Message)); } var user = new Technician { Name = Input.Name, BirthDate = Input.BirthDate, UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var roleToAdd = await _roleManager.FindByNameAsync(IdentityData.NonAdminRoleNames[this.Role]); _userManager.AddToRoleAsync(user, roleToAdd.Name).Wait(); // Es necesario tener acceso a RoleManager para poder buscar el rol de este usuario; se asigna aquí para poder // buscar por rol después cuando no hay acceso a RoleManager. user.AssignRole(_userManager, roleToAdd.Name); await _userManager.UpdateAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }