Пример #1
0
        public async Task <ActionResult> Login(LoginModel model)
        {
            ActionResult response = null;

            var errors = new List <string>();

            if (string.IsNullOrWhiteSpace(model.LoginEmail))
            {
                errors.Add("Login email is invalid");
            }

            if (!Regex.IsMatch(model.LoginEmail, @".*@.*\..*"))
            {
                errors.Add("Login email must contains \'@\', \'.\', symbols successively");
            }

            if (model.Password.Length < 1)
            {
                errors.Add("Password must be at least 1 symbol");
            }

            if (!await _securityAdapter.UserExists(model.LoginEmail) ||
                !await _securityAdapter.CheckPassword(model.LoginEmail, model.Password))
            {
                errors.Add("Invalid username or password");
            }

            bool loginSuccess = false;

            if (!errors.Any())
            {
                loginSuccess = await _securityAdapter.Login(model.LoginEmail, model.Password, model.RememberMe);
            }

            if (loginSuccess)
            {
                response = StatusCode((int)HttpStatusCode.OK, new { model.LoginEmail, model.ReturnUrl });
            }
            else
            {
                response = StatusCode((int)HttpStatusCode.BadRequest, errors);
            }

            return(response);
        }