Example #1
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model, string returnUrl = null)
        {
            var result = new LoginResponse();

            if (!ModelState.IsValid)
            {
                result.MapErrorsFromModelState(ModelState);
                return(Json(result));
            }

            var signInTask = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, lockoutOnFailure : false);

            if (signInTask.Succeeded)
            {
                _logger.LogInformation(1, "User logged in.");

                var user = await _userManager.FindByNameAsync(model.UserName);

                user.LastLoginDate = DateTime.UtcNow;

                var savingChangesTask = _context.SaveChangesAsync();

                result.User = new LoginResponse._User
                {
                    Name = user.UserName
                };
                result.Success = true;

                await savingChangesTask;
            }
            //if (signInTask.IsLockedOut)
            //{
            //    _logger.LogWarning(2, "User account locked out.");
            //}
            else
            {
                result.AddGeneralError("Sorry, no luck logging in.");
            }

            return(Json(result));
        }