Beispiel #1
0
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                if (model.IsModal)
                {
                    return(View("LoginModal", model));
                }

                else
                {
                    return(View(model));
                }
            }
            _logger.LogInformation("User {0} is going to login ", model.UserName);

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

            if (user == null)
            {
                user = await _userManager.FindByEmailAsync(model.UserName.ToLower());
            }
            if (user != null && user.EmailConfirmed)
            {
                //var claims = await _userManager.GetClaimsAsync(user);
                // claims.Add(new System.Security.Claims.Claim("companyid", "44"));

                var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.IsRemember, true);

                if (result.Succeeded)
                {
                    var validation = _companyuser_repo.ValidateUserOnLogin(user);
                    if (validation > 0)   // required refresh of claims
                    {
                        await _signInManager.RefreshSignInAsync(user);
                    }
                    if (validation < 0)   // probably not allow to login
                    {
                    }
                    if (model.IsModal)
                    {
                        return(Ok(new { res = "OK", returnUrl = string.IsNullOrEmpty(model.ReturnUrl) ? Url.Action("Index", "Home") : model.ReturnUrl }));
                        //Task.FromResult(Json(new { res="OK",ReturnUrl= string.IsNullOrEmpty(model.ReturnUrl) ? Url.Content("~") : model.ReturnUrl }))
                    }
                    if (string.IsNullOrEmpty(model.ReturnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    return(Redirect(model.ReturnUrl));
                }
                //if(user.AccessFailedCount >= 3)
                //{
                //    ModelState.AddModelError("", "Contact to admin to unlock your account");
                //}
                //user.AccessFailedCount += 1;
                //await _companyuser_repo.PostUpdateUserAsync(user, true);
                if (result.IsLockedOut)
                {
                    ModelState.AddModelError("", _localizer.GetLocalizedString("UserLockedOut"));
                    _logger.LogWarning("The  user {0} is Locked out", model.UserName);
                }
                else
                {
                    ModelState.AddModelError("", _localizer.GetLocalizedString("IncorrectPassword"));
                    _logger.LogWarning("The password for user {0} is invalid", model.UserName);
                }
                _logger.LogWarning("The password for user {0} is invalid", model.UserName);
                return(View("LoginModal", model));
            }
            if (user != null && !user.EmailConfirmed)
            {
                _logger.LogWarning("User: {0} hasn't confirmed Email: {1}", model.UserName, user.Email);
                ModelState.AddModelError("", _localizer.GetLocalizedString("You have to confirm your Email before"));
                return(View("LoginModal", model));
            }
            if (user == null)
            {
                _logger.LogWarning("Can't find registered user {0}", model.UserName);
                ModelState.AddModelError("", _localizer.GetLocalizedString("UserNotFound"));
                return(View("LoginModal", model));
            }

            if (model.IsModal)
            {
                return(PartialView("LoginModal", model));
            }
            else
            {
                return(View(model));
            }
        }