private void CreateUser(IServiceProvider serviceProvider, string email, string nome, string username, string password, string contato, string dataNascimento, string role) { var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); Task <ApplicationUser> testUser = userManager.FindByEmailAsync(email); testUser.Wait(); if (testUser.Result == null) { ApplicationUser testuser = new ApplicationUser { Email = email, Nome = nome, UserName = username, Contato = contato, DataNascimento = dataNascimento, PasswordHashAux = PasswordHashExtensions.Encode(password), EmailConfirmed = true }; Task <IdentityResult> newUser = userManager.CreateAsync(testuser, password); newUser.Wait(); Task <IdentityResult> newUserRole = userManager.AddToRoleAsync(testuser, role); newUserRole.Wait(); } }
public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { SetErrorMessage("003"); return(View(model)); } var user = await _userManager.FindByIdAsync(model.UserId); if (user == null) { // Don't reveal that the user does not exist return(RedirectToAction(nameof(ResetPasswordConfirmation))); } user.PasswordHashAux = PasswordHashExtensions.Encode(model.Password); var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password); if (result.Succeeded) { await _userManager.UpdateAsync(user); return(RedirectToAction(nameof(ResetPasswordConfirmation))); } else { SetErrorMessage("005"); } SetHelpTooltipsResetPassword(); SetHelpModal("RecoverPassword"); AddErrors(result); return(View()); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { Nome = model.Nome, DataNascimento = model.DataNascimento, Email = model.Email, Contato = model.Contato, UserName = model.Username, PasswordHashAux = PasswordHashExtensions.Encode(model.Password) }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); var newUserRole = await _userManager.AddToRoleAsync(user, "Estudante"); SetSuccessMessage("Conta criada com sucesso. Note que esta tem que ser ativada no email antes de poder ser utilizada."); //await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation("User created a new account with password."); return(RedirectToAction("Login")); } else { if (result.Errors.Where(r => r.Code == "DuplicateUsername").ToList().Count != 0) { SetErrorMessage("004"); } else { SetErrorMessage("003"); } AddErrors(result); } } else { SetErrorMessage("003"); } SetHelpTooltipsRegisto(); SetHelpModal("Register"); // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> ChangePassword(IndexViewModel model) { SetHelpModal("Index"); SetHelpToolTips(); if (!ModelState.IsValid) { SetErrorMessage("003"); return(View("Index", model)); } var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } model.ChangeDetails = new ChangeDetailsViewModel { Nome = user.Nome, DataNascimento = user.DataNascimento, Email = user.Email, Contato = user.Contato }; var changePasswordResult = await _userManager.ChangePasswordAsync(user, model.ChangePassword.OldPassword, model.ChangePassword.NewPassword); if (!changePasswordResult.Succeeded) { SetErrorMessage("003"); AddErrorString("A password inserida não corresponde à password da conta!"); return(View("Index", model)); } user.PasswordHashAux = PasswordHashExtensions.Encode(model.ChangePassword.NewPassword); await _userManager.UpdateAsync(user); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User changed their password successfully."); SetSuccessMessage("A sua password foi alterada."); return(RedirectToAction(nameof(Index))); }