public async Task <IActionResult> SignIn(SignInViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, AccountContent.InvalidUsernameErrorText);
                return(View(model));
            }

            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                ModelState.AddModelError(string.Empty, AccountContent.EmailConfirmationRequiredErrorText);
                return(View(model));
            }

            var signInResult = await _signInManager.PasswordSignInAsync(model.Email, model.Password, true, false);

            if (signInResult.Succeeded)
            {
                _logger.LogInformation(1, "User logged in.");
                return(Redirect(string.IsNullOrWhiteSpace(model.ReturnUrl) ? "/" : model.ReturnUrl));
            }

            if (signInResult.IsNotAllowed)
            {
                _logger.LogWarning(2, "The user account is not activated");
                await SendActivationEmail(user, model.ReturnUrl);

                return(RedirectToAction(nameof(RegistrationComplete), new
                {
                    protectedSequence = _protector.Protect(_urlEncoderWrapper.UrlEncode(model.Email))
                }));
            }

            if (signInResult.IsLockedOut)
            {
                _logger.LogWarning(2, "User account locked out.");
                return(View("Lockout"));
            }

            ModelState.AddModelError(string.Empty, AccountContent.InvalidLoginAttemptText);
            return(View(model));
        }
示例#2
0
        public async Task <CustomeSignInResult> LogInUserAsync(Employer user, string password)
        {
            if (_employerHandler.IsAccessable(user))
            {
                var result = await _signInManager.PasswordSignInAsync(user, password, true, false);

                if (result.Succeeded)
                {
                    var token = _tokenHandler.GenerateToken(user);
                    _httpContextAccessor.HttpContext.Response.Cookies.Append(".AspUser",
                                                                             _dataProtectionProvider.CreateProtector(_configuration["dataprotector"]).Protect(token),
                                                                             new CookieOptions()
                    {
                        Expires = DateTime.Now.AddDays(.6)
                    });
                    return(new CustomeSignInResult()
                    {
                        IsLockedOut = result.IsLockedOut,
                        Succeeded = result.Succeeded,
                        IsNotAllowed = result.IsNotAllowed,
                        RequiresTwoFactor = result.RequiresTwoFactor,
                        Errors = (await _employerHandler.LastcheckInAsync(user)).Errors.ToList(),
                        Token = token
                    });
                }
            }
            return(new CustomeSignInResult()
            {
                Errors = new List <IdentityError>()
                {
                    new IdentityError()
                    {
                        Code = "400",
                        Description = "Contact to the shopmanger"
                    },
                    new IdentityError()
                    {
                        Code = "304",
                        Description = "Contact to the inernet"
                    }
                },
                IsLockedOut = false,
                RequiresTwoFactor = false,
                IsNotAllowed = false,
                Succeeded = false,
                Token = null
            });
        }
 public async Task <SignInResult> PasswordSignInAsync(string email, string password, bool rememberMe, bool lockoutOnFailure)
 {
     return(await _signInManager.PasswordSignInAsync(email, password, rememberMe, lockoutOnFailure));
 }