public async Task <IActionResult> Create(CreateModel model) { if (ModelState.IsValid) { ApplicationUSer user = new ApplicationUSer { UserName = model.Name, Email = model.Email }; IdentityResult result = await UserMana.CreateAsync(user, model.Password); if (result.Succeeded) { return(RedirectToAction("Index")); } else { foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } } return(View(model)); }
public async Task <IActionResult> Edit(string id) { ApplicationUSer user = await UserMana.FindByIdAsync(id); if (user != null) { return(View(user)); } else { return(RedirectToAction("Index")); } }
public async Task <IActionResult> Edit(string id, string email, string password) { ApplicationUSer user = await UserMana.FindByIdAsync(id); if (user != null) { user.Email = email; IdentityResult validEmail = await userValidator.ValidateAsync(UserMana, user); if (!validEmail.Succeeded) { AddErrorsFromResult(validEmail); } IdentityResult validPass = null; if (!string.IsNullOrEmpty(password)) { validPass = await passwordValidator.ValidateAsync(UserMana, user, password); if (validPass.Succeeded) { user.PasswordHash = passwordHasher.HashPassword(user, password); } else { AddErrorsFromResult(validPass); } } if ((validEmail.Succeeded && validPass == null) || (validEmail.Succeeded && password != string.Empty && validPass.Succeeded)) { IdentityResult result = await UserMana.UpdateAsync(user); if (result.Succeeded) { return(RedirectToAction("Index")); } else { AddErrorsFromResult(result); } } } else { ModelState.AddModelError("", "User Not Found"); } return(View(user)); }
public async Task <IActionResult> Edit(RoleModificationModel model) { IdentityResult result; if (ModelState.IsValid) { foreach (string userId in model.IdsToAdd ?? new string[] { }) { ApplicationUSer user = await userManager.FindByIdAsync(userId); if (user != null) { result = await userManager.AddToRoleAsync(user, model.RoleName); if (!result.Succeeded) { AddErrorsFromResult(result); } } } foreach (string userId in model.IdsToDelete ?? new string[] { }) { ApplicationUSer user = await userManager.FindByIdAsync(userId); if (user != null) { result = await userManager.RemoveFromRoleAsync(user, model.RoleName); if (!result.Succeeded) { AddErrorsFromResult(result); } } } } if (ModelState.IsValid) { return(RedirectToAction(nameof(Index))); } else { return(await Edit(model.RoleId)); } }
public async Task <IActionResult> Login(LoginModel details, string returnUrl) { if (ModelState.IsValid) { ApplicationUSer user = await userManager.FindByEmailAsync(details.Email); if (user != null) { await signInManager.SignOutAsync(); Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(user, details.Password, false, false); if (result.Succeeded) { return(Redirect(returnUrl ?? "AccueilCI")); } } ModelState.AddModelError(nameof(LoginModel.Email), "Invalid user or password"); } return(View(details)); }
public async Task <IActionResult> Delete(string id) { ApplicationUSer user = await UserMana.FindByIdAsync(id); if (user != null) { IdentityResult result = await UserMana.DeleteAsync(user); if (result.Succeeded) { return(RedirectToAction("Index")); } else { AddErrorsFromResult(result); } } else { ModelState.AddModelError("", "Error Not Found"); } return(RedirectToAction("Index", UserMana.Users)); }