示例#1
0
        public async Task <IActionResult> ValidatePassword([FromBody] ValidatePasswordRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            var userNameWasProvided = !string.IsNullOrWhiteSpace(request.UserName);
            var availableRules      = GetAvailableRules(userNameProvided: !string.IsNullOrWhiteSpace(request.UserName)).ToDictionary(x => x.Key, x => new PasswordRuleInfo {
                Name        = x.Key,
                IsValid     = true,
                Description = x.Value
            });
            var user = new User {
                UserName = request.UserName ?? string.Empty, Id = User.FindSubjectId() ?? string.Empty
            };

            foreach (var validator in _userManager.PasswordValidators)
            {
                var result = await validator.ValidateAsync(_userManager, user, request.Password);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        if (availableRules.ContainsKey(error.Code))
                        {
                            availableRules[error.Code].IsValid = false;
                        }
                    }
                }
            }
            return(Ok(new CredentialsValidationInfo {
                PasswordRules = availableRules.Values.ToList()
            }));
        }
示例#2
0
        public async Task <IActionResult> ValidatePassword([FromBody] ValidatePasswordRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            User user = null;

            if (!string.IsNullOrWhiteSpace(request.Token) && Base64Id.TryParse(request.Token, out var userId))
            {
                user = await _userManager.FindByIdAsync(userId.Id.ToString());
            }
            var userAvailable     = user != null;
            var userNameAvailable = !string.IsNullOrWhiteSpace(request.UserName);
            var availableRules    = GetAvailableRules(userAvailable, userNameAvailable).ToDictionary(rule => rule.Key, rule => new PasswordRuleInfo {
                Code        = rule.Key,
                IsValid     = true,
                Description = rule.Value.Description,
                Requirement = rule.Value.Hint
            });

            foreach (var validator in _userManager.PasswordValidators)
            {
                var result = await validator.ValidateAsync(_userManager, user ?? new User(), request.Password ?? string.Empty);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        if (availableRules.ContainsKey(error.Code))
                        {
                            availableRules[error.Code].IsValid = false;
                        }
                    }
                }
            }
            return(Ok(new CredentialsValidationInfo {
                PasswordRules = availableRules.Values.ToList()
            }));
        }