public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model) { try { _log.Info(LogMessageHelper.LogFormattedMessageForRequestStart("ForgotPassword[POST]", $"UserEmail: {model.Email}")); if (ModelState.IsValid) { bool isValid = true; var user = await _userManager.FindByNameAsync(model.Email); if (user == null) { isValid = false; // Don't reveal that the user does not exist ModelState.AddModelError(string.Empty, "Invalid email."); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("ForgotPassword[POST]", $"Invalid email, UserEmail: {model.Email}")); return(View(model)); } //var isEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); //if (!isEmailConfirmed) //{ // isValid = false; // // Don't reveal that the user email is not confirmed // ModelState.AddModelError(string.Empty, "Email is not confirmed."); // _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("ForgotPassword[POST]", $"Email is not confirmed, UserEmail: {model.Email}")); // return View(model); //} if (isValid) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link var message = await GenareteForgotPasswordEmailTemplateAsync(user); //await _emailSender.SendEmailBySendGridAsync(user.Id, model.Email, "Reset Password", message); return(View("ForgotPasswordConfirmation")); } } } catch (Exception ex) { ModelState.AddModelError(string.Empty, MessageHelper.Error); _log.Error(LogMessageHelper.FormateMessageForException(ex, "ForgotPassword[POST]")); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model) { try { _log.Info(LogMessageHelper.LogFormattedMessageForRequestStart("ResetPassword[POST]", $"UserEmail: {model.Email}")); if (ModelState.IsValid) { bool isValid = true; var user = await _userManager.FindByNameAsync(model.Email); if (user == null) { isValid = false; // Don't reveal that the user does not exist ModelState.AddModelError(string.Empty, "Invalid email."); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("ResetPassword[POST]", $"Invalid email, UserEmail: {model.Email}")); return(View(model)); } //var isEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user); //if (!isEmailConfirmed) //{ // isValid = false; // // Don't reveal that the user email is not confirmed // ModelState.AddModelError(string.Empty, "Email is not confirmed."); // _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("ResetPassword[POST]", $"Email is not confirmed, UserEmail: {model.Email}")); // return View(model); //} if (isValid) { var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password); if (result.Succeeded) { return(View("ResetPasswordConfirmation")); } } } } catch (Exception ex) { ModelState.AddModelError(string.Empty, MessageHelper.Error); _log.Error(LogMessageHelper.FormateMessageForException(ex, "ResetPassword[POST]")); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Logout() { try { _log.Info(LogMessageHelper.LogFormattedMessageForRequestStart("LogOff", $"User:{User.Identity.Name}")); await _signInManager.SignOutAsync(); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("LogOff", $"User logged out")); } catch (Exception ex) { _log.Error(LogMessageHelper.FormateMessageForException(ex, "LogOff")); } return(RedirectToAction("Login", "Account")); }
public async Task <IActionResult> Login(LoginViewModel model) { try { _log.Info(LogMessageHelper.LogFormattedMessageForRequestStart("Login[POST]", $"UserEmail: {model.Email}")); if (ModelState.IsValid) { ApplicationUser user = await _userManager.FindByNameAsync(model.Email); if (user != null) { await _signInManager.SignOutAsync(); // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false); if (result.Succeeded) { //var data = await GetUserSignInOutHistoryLocal(model); //await _userLogInOutHistoryManager.CreateLogInOutHistoryAsync(data); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Login[POST]", $"User logged in, UserEmail: {model.Email}")); var isAdmin = await _userManager.IsInRoleAsync(user, AppConstants.AppUserRole.Admin); if (isAdmin) { if (string.IsNullOrEmpty(model.ReturnUrl)) { return(RedirectToAction("Index", "Admin")); } return(Redirect(model.ReturnUrl)); } else { return(RedirectToLocal(model.ReturnUrl)); } } if (result.IsLockedOut) { _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Login[POST]", $"User account locked out, UserEmail: {model.Email}")); return(View("Lockout")); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Login[POST]", $"Invalid login attempt, UserEmail: {model.Email}")); return(View(model)); } } } } catch (Exception ex) { ModelState.AddModelError(string.Empty, MessageHelper.Error); _log.Error(LogMessageHelper.FormateMessageForException(ex, "Login[POST]")); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel model) { try { _log.Info(LogMessageHelper.LogFormattedMessageForRequestStart("Register[POST]")); if (ModelState.IsValid) { string userName = ((model.Email).Split('@')[0]).Trim(); // you are get here username. var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; _result = await IsEmailExists(user); if (!_result.Success) { ModelState.AddModelError(string.Empty, _result.Error); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Register[POST]", _result.Error)); return(View(model)); } var role = new ApplicationRole { Id = model.RoleName, Name = model.RoleName, IsActive = true }; IdentityResult resultRole = await _roleManager.CreateAsync(role); if (resultRole.Succeeded) { var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, model.RoleName); await _signInManager.SignInAsync(user, isPersistent : false); _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Register[POST]", $"User created a new account with password, UserEmail:{model.Email}")); var isAdmin = await _userManager.IsInRoleAsync(user, AppConstants.AppUserRole.Admin); if (isAdmin) { if (string.IsNullOrEmpty(model.ReturnUrl)) { return(RedirectToAction("Index", "Admin")); } return(Redirect(model.ReturnUrl)); } else { return(RedirectToLocal(model.ReturnUrl)); } } _log.Info(LogMessageHelper.LogFormattedMessageForRequestSuccess("Register[POST]", $"User creation failed, UserEmail:{model.Email}")); AddErrors(result); } } } catch (Exception ex) { ModelState.AddModelError(string.Empty, MessageHelper.Error); _log.Error(LogMessageHelper.FormateMessageForException(ex, "Register[POST]")); } // If we got this far, something failed, redisplay form return(View(model)); }