public async Task <string> Login([FromBody] LoginRequest request)
        {
            var user = await _userManager.FindByNameAsync(request.UserName);

            var loginResult = await _userManager.CheckPasswordAsync(user, request.Password);

            return(loginResult ? "登录成功" : "用户名或密码错误");
        }
Exemplo n.º 2
0
        public async Task <TokenAuthorizeModel> AuthorizeAsync(MyUserManager userManager, LoginViewModel viewModel)
        {
            MyIdentity identity = viewModel.ToEntity();
            var        user     = await userManager.FindByNameAsync(identity.UserName);

            if (user == null)
            {
                throw new FoodCourtException(ErrorMessage.USER_IS_NOT_EXIST);
            }

            bool result = await userManager.CheckPasswordAsync(user, viewModel.Password);

            if (result)
            {
                return(await user.AuthorizeAsync(userManager, _extensionSettings.appSettings));
            }
            throw new FoodCourtException(ErrorMessage.PASSWORD_NOT_VALID);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Comprueba el inicio de sesión de un usuario y devuelve un estado que indica si ha sido exitoso o no. De no serlo, la respuesta indica la razón.
        /// </summary>
        /// <param name="username">Nombre de usuario.</param>
        /// <param name="password">Contraseña.</param>
        /// <param name="skipEmailConfirmed">¿Debe validarse que el email haya sido confirmado?</param>
        /// <returns></returns>
        public async Task <LogInResult> CheckCredentialsAsync(string username, string password, bool skipEmailConfirmed = false)
        {
            LogInResult result = new LogInResult(LoginStatusTypes.UnknownError);

            try
            {
                var user = await uM.FindByNameAsync(username);

                result = new LogInResult(LoginStatusTypes.UnknownError, user);
                if (user == null)
                {
                    //No existe
                    result.Status = LoginStatusTypes.DoesNotExist;
                    return(result);
                }

                if (user.LockoutEnabled)
                {
                    bool haltAndReturn = true;
                    //Usuario bloqueado
                    if (user.LockoutEnd.HasValue)
                    {
                        //El bloqueo es temporal.
                        if (user.LockoutEnd.Value <= DateTimeOffset.Now)
                        {
                            //El bloqueo ha expirado: desbloquear & continuar.
                            await uM.SetLockoutEnabledAsync(user, false);

                            haltAndReturn = false;
                        }
                        else
                        {
                            ///El bloqueo sigue activo.
                            result.Status = LoginStatusTypes.UserLocked_Temporal;
                        }
                    }
                    else
                    {
                        //El bloqueo es permanente
                        result.Status = LoginStatusTypes.UserLocked_Permanent;
                    }
                    if (haltAndReturn)
                    {
                        return(result);
                    }
                }

                if (!skipEmailConfirmed)
                {
                    if (!user.EmailConfirmed)
                    {
                        result.Status = LoginStatusTypes.EmailNotConfirmed;
                    }
                    return(result);
                }

                bool isPasswordValid = await uM.CheckPasswordAsync(user, password);

                if (!isPasswordValid)
                {
                    result.Status = LoginStatusTypes.PasswordNotValid;
                    return(result);
                }
                else
                {
                    result.Status = LoginStatusTypes.Success;
                }
            }
            catch (Exception)
            {
            }

            return(result);
        }