public async Task <IActionResult> SendPasswordResetLink(RequestPasswordResetViewModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var user = await UserManagerAgent.FindByNameAsync(model.UserName).ConfigureAwait(false); if (user == null || !await UserManagerAgent.IsEmailConfirmedAsync(user).ConfigureAwait(false)) { ViewBag.Message = "Error while resetting your password!"; return(View("Error")); } var token = await UserManagerAgent.GeneratePasswordResetTokenAsync(user).ConfigureAwait(false); var resetLink = Url.Action("ResetPassword", "Account", new { token }, protocol: HttpContext.Request.Scheme); // code to email the above link string[] emailAddresses = { _appSettings.SMTP.AdminEmail, user.Email }; await _emailAgent.SendEmailAsync(_appSettings.SMTP.FromEmail, _appSettings.SMTP.FromEmail, emailAddresses, "Winemakers Software - Password Reset.", CreatePasswordResetEmail(resetLink), true, null).ConfigureAwait(false); // redirect to limbo page return(RedirectToAction("PasswordLimbo", "Account")); }
public async Task <IActionResult> EditUser(UserViewModel model) { ViewData["Title"] = "Edit a User"; var user = await UserManagerAgent.FindByNameAsync(model?.UserName).ConfigureAwait(false); model = _mapper.Map <UserViewModel>(user); model.MemberRoles.AddRange(await UserManagerAgent.GetRolesAsync(user).ConfigureAwait(false)); model.AllRoles.AddRange(GetAllRolesAsSelectList()); return(View(model)); }
public async Task <IActionResult> DeleteUser(string UserName) { var user = await UserManagerAgent.FindByNameAsync(UserName).ConfigureAwait(false); if (user != null) { if (!await UserManagerAgent.IsInRoleAsync(user, _appSettings.SecRole.Admin).ConfigureAwait(false)) { await UserManagerAgent.DeleteAsync(user).ConfigureAwait(false); } } return(RedirectToAction("Index", "Admin", new { id = "users" })); }
public async Task <IActionResult> DeleteUserRole(UserViewModel model) { ViewData["Title"] = "Edit a User"; var user = await UserManagerAgent.FindByNameAsync(model?.UserName).ConfigureAwait(false); if (await RoleManagerAgent.RoleExistsAsync(model.NewRole).ConfigureAwait(false) && await UserManagerAgent.IsInRoleAsync(user, model.NewRole).ConfigureAwait(false)) { await UserManagerAgent.RemoveFromRoleAsync(user, model.NewRole).ConfigureAwait(false); } model = _mapper.Map <UserViewModel>(user); model.MemberRoles.AddRange(await UserManagerAgent.GetRolesAsync(user).ConfigureAwait(false)); model.AllRoles.AddRange(GetAllRolesAsSelectList()); return(View("EditUser", model)); }
public async Task <IActionResult> UnlockUser(string UserName) { var user = await UserManagerAgent.FindByNameAsync(UserName).ConfigureAwait(false); if (await UserManagerAgent.IsLockedOutAsync(user).ConfigureAwait(false)) { var result = await UserManagerAgent.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow).ConfigureAwait(false); if (result.Succeeded) { await UserManagerAgent.ResetAccessFailedCountAsync(user).ConfigureAwait(false); } } return(RedirectToAction("Index", "Admin", new { id = "users" })); }
public async Task <IActionResult> Login(LoginViewModel model, Uri returnUrl = null) { ViewData["Title"] = "Login"; // ViewData["Menu"] = "navAccount"; ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = await UserManagerAgent.FindByNameAsync(model?.UserName).ConfigureAwait(false); if (user != null) { // check that user has validated email address if (!await UserManagerAgent.IsEmailConfirmedAsync(user).ConfigureAwait(false)) { ModelState.AddModelError(string.Empty, "Email has not yet been verified! Please check your Email Inbox and click Verify."); return(View(model)); } var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, isPersistent : true, lockoutOnFailure : false).ConfigureAwait(false); if (result.Succeeded) { await UserManagerAgent.ResetAccessFailedCountAsync(user).ConfigureAwait(false); return(RedirectToLocal(returnUrl?.ToString())); } if (result.IsLockedOut) { var availableNext = user.LockoutEnd.Value.ToLocalTime().ToString("g", CultureInfo.CurrentCulture); ModelState.AddModelError("", string.Format(CultureInfo.CurrentCulture, "Due to multiple failed login attempts, your account has been locked out until {0}", availableNext)); } else { await UserManagerAgent.AccessFailedAsync(user).ConfigureAwait(false); ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(View(model)); } } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var user = await UserManagerAgent.FindByNameAsync(model.UserName).ConfigureAwait(false); IdentityResult result = await UserManagerAgent.ResetPasswordAsync(user, model.Token, model.Password).ConfigureAwait(false); if (result.Succeeded) { Success(_localizer["PasswordResetSuccess"], true); return(View("Login")); } else { Danger(_localizer["PasswordResetFail"]); return(View("RequestNewPassword")); } }
public async Task <IActionResult> LockUser(string UserName, DateTimeOffset?timeOut = null) { var user = await UserManagerAgent.FindByNameAsync(UserName).ConfigureAwait(false); if (!await UserManagerAgent.IsLockedOutAsync(user).ConfigureAwait(false)) { var result = await UserManagerAgent.SetLockoutEnabledAsync(user, true).ConfigureAwait(false); if (result.Succeeded) { if (timeOut.HasValue) { await UserManagerAgent.SetLockoutEndDateAsync(user, timeOut).ConfigureAwait(false); } else { await UserManagerAgent.SetLockoutEndDateAsync(user, DateTimeOffset.MaxValue).ConfigureAwait(false); } } } return(RedirectToAction("Index", "Admin", new { id = "users" })); }