Esempio n. 1
0
        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"));
        }
Esempio n. 2
0
        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));
        }