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