コード例 #1
0
        public async Task <IActionResult> Post([FromBody] CredentialsViewModel credentials)
        {
            CredentialsViewModelValidator validator = new CredentialsViewModelValidator();

            FluentValidation.Results.ValidationResult results = validator.Validate(credentials);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Checks if Username Password combination is correct.
            var identity = await GetClaimsIdentity(credentials.EmailAddress, credentials.UserPassword);

            if (identity == null)
            {
                return(BadRequest(Errors.AddErrorToModelState("login_failure", "Onjuiste email of wachtwoord.", ModelState)));
            }

            // Generates Token
            var jwt = await Tokens.GenerateJwt(identity, _jwtGenerator, credentials.EmailAddress, _jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

            return(new OkObjectResult(jwt));
        }
コード例 #2
0
        public void CredentialsViewModelValidator()
        {
            var validator = new CredentialsViewModelValidator();

            validator.ShouldHaveValidationErrorFor(e => e.UserName, string.Empty);
            validator.ShouldHaveValidationErrorFor(e => e.Password, string.Empty);
            validator.ShouldNotHaveValidationErrorFor(e => e.Password, "123456");
            validator.ShouldNotHaveValidationErrorFor(e => e.Password, "123456789");
            validator.ShouldNotHaveValidationErrorFor(e => e.Password, "123456789112");
            validator.ShouldHaveValidationErrorFor(e => e.Password, "1234567891123");
        }
コード例 #3
0
        public async Task <IActionResult> Post([FromBody] CredentialsViewModel credentials)
        {
            var validator = new CredentialsViewModelValidator();
            var result    = validator.Validate(credentials);

            if (!ModelState.IsValid || !result.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var identity = await GetClaimsIdentity(credentials.UserName, credentials.Password);

            if (identity == null)
            {
                return(BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState)));
            }

            var jwt = await Tokens.GenerateJwt(identity, _jwtFactory, credentials.UserName, _jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

            return(new OkObjectResult(jwt));
        }