コード例 #1
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (_userSettings.UsernamesEnabled && model.Username != null)
                {
                    model.Username = model.Username.Trim();
                }

                #region First time login

                var userFirstTime = _userSettings.UsernamesEnabled
                    ? await _userService.GetUserByUsernameAsync(model.Username)
                    : await _userService.GetUserByEmailAsync(model.Email);

                if (userFirstTime == null)
                {
                    ModelState.AddModelError(string.Empty, "Unknown user account");
                    _notificationService.ErrorNotification("Unknown user account");

                    model = await _userAccountModelFactory.PrepareLoginModel();

                    return(View(model));
                }

                if (userFirstTime.LastLoginDateUtc == null)
                {
                    return(RedirectToAction("FirstTimeLogin", new { id = userFirstTime.Id }));
                }

                #endregion

                var loginResult = await _userRegistrationService
                                  .ValidateUserAsync(_userSettings.UsernamesEnabled?model.Username : model.Email, model.Password);

                switch (loginResult)
                {
                case UserLoginResults.Successful:
                {
                    var user = _userSettings.UsernamesEnabled
                                ? await _userService.GetUserByUsernameAsync(model.Username)
                                : await _userService.GetUserByEmailAsync(model.Email);

                    //sign in new user
                    await _authenticationService.SignInAsync(user, model.RememberMe);

                    //activity log
                    await _userActivityService.InsertActivityAsync(user, "Login", $"Login ('{user.Username}')", user);

                    if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
                    {
                        return(RedirectToRoute("HomePage"));
                    }

                    return(Redirect(returnUrl));
                }

                case UserLoginResults.UserNotExist:
                    ModelState.AddModelError(string.Empty, "No user account found");
                    _notificationService.ErrorNotification("No user account found");
                    break;

                case UserLoginResults.Deleted:
                    ModelState.AddModelError(string.Empty, "User is deleted");
                    _notificationService.ErrorNotification("User is deleted");
                    break;

                case UserLoginResults.NotActive:
                    ModelState.AddModelError(string.Empty, "Account is not active");
                    _notificationService.ErrorNotification("Account is not active");
                    break;

                case UserLoginResults.NotRegistered:
                    ModelState.AddModelError(string.Empty, "Account is not registered");
                    _notificationService.ErrorNotification("Account is not registered");
                    break;

                case UserLoginResults.LockedOut:
                    ModelState.AddModelError(string.Empty, "User is locked out");
                    _notificationService.ErrorNotification("User is locked out");
                    break;

                default:
                    ModelState.AddModelError(string.Empty, "The credentials provided are incorrect");
                    _notificationService.ErrorNotification("The credentials provided are incorrect");
                    break;
                }
            }

            //If we got this far, something failed, redisplay form
            model = await _userAccountModelFactory.PrepareLoginModel();

            return(View(model));
        }