public void StringRuleSet3Test_TError(StringDTO subject, string[] expected)
        {
            validatorWithError = new StringRulesValidator_TError(3);
            var actual = validatorWithError.Validate(subject);

            Assert.Equal(expected, actual.Errors);
        }
        public void StringRuleSet3Test(StringDTO subject, bool expected)
        {
            validator = new StringRulesValidator(3);
            var actual = validator.Validate(subject);

            Assert.Equal(expected, actual);
        }
Exemple #3
0
        private void onButtonClicked(ButtonPressedEventArgs e, StringDTO nameToRemove, Action <string> deleteAction)
        {
            var pressedButton = e.Button;

            if (!pressedButton.Kind.Equals(ButtonPredefines.Delete))
            {
                return;
            }

            deleteAction(nameToRemove.Print);
        }
Exemple #4
0
        public async Task <IActionResult> RecoverUsername([FromBody] StringDTO stringModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    _logger.LogWarning("User attempted to recover username with invalid model state.");
                    return(BadRequest("Request failed - invalid request"));
                }

                var email = stringModel.Value;
                if (email == null)
                {
                    _logger.LogWarning("User attempted to recover username with missing email.");
                    return(BadRequest("Request failed - missing parameters."));
                }

                var user = await _unitOfWork.UserManager.FindByEmailAsync(email).ConfigureAwait(false);

                if (user == null)
                {
                    _logger.LogInformation("User attempted to recover username with invalid email.");
                    return(NoContent());
                }

                var emailData = new UsernameTemplate()
                {
                    Username = user.UserName
                };
                var emailResponse = await _emailSender
                                    .SendEmailAsync(user.Email, _config["SendGrid:Templates:UsernameRecovery"], EmailFrom.Account, emailData)
                                    .ConfigureAwait(false);

                if (emailResponse.StatusCode != System.Net.HttpStatusCode.Accepted)
                {
                    _logger.LogError($"SendGrid failed to send username recovery notification to {user.UserName}.");
                    return(StatusCode(500, "Username recovery unsuccessful."));
                }
                else
                {
                    _logger.LogInformation($"Username recovery confirmation email dispatched to {user.UserName}.");
                    return(NoContent());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception thrown attempting to recover username.");
                return(StatusCode(500, "Username recovery unsuccessful."));
            }
        }
Exemple #5
0
        public async Task <IActionResult> RequestPasswordReset([FromBody] StringDTO stringModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    _logger.LogWarning("User attempted to reset password with invalid model.");
                    return(BadRequest("Request failed - invalid request"));
                }
                var recoveryString = stringModel.Value;
                if (recoveryString == null)
                {
                    _logger.LogInformation("User attempted to reset password with missing parameter.");
                    return(BadRequest("Request failed - missing parameters."));
                }

                var email = recoveryString.Contains("@");
                var user  = email ?
                            await _unitOfWork.UserManager.FindByEmailAsync(recoveryString).ConfigureAwait(false) :
                            await _unitOfWork.UserManager.FindByNameAsync(recoveryString).ConfigureAwait(false);

                if (user == null)
                {
                    _logger.LogWarning("Password reset attempted for invalid user.");
                    if (email)
                    {
                        return(NoContent());
                    }
                    else
                    {
                        return(StatusCode(500, "Password reset request unsuccessful."));
                    }
                }

                var token = await _unitOfWork.UserManager.GeneratePasswordResetTokenAsync(user).ConfigureAwait(false);

                token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));

                var parameters = new string[] { "userId", user.Id, "token", token };
                var emailData  = new UsernameLinkTemplate()
                {
                    Username = user.UserName,
                    HrefLink = HtmlEncoder.Default.Encode(GetBaseURL("password-reset", parameters))
                };

                var emailResponse = await _emailSender
                                    .SendEmailAsync(user.Email, _config["SendGrid:Templates:PasswordRecoveryRequest"], EmailFrom.Account, emailData)
                                    .ConfigureAwait(false);

                if (emailResponse.StatusCode != System.Net.HttpStatusCode.Accepted)
                {
                    _logger.LogError($"SendGrid failed to send password recovery notification to user {user.UserName}.");
                    return(StatusCode(500, "Password reset request unsuccessful."));
                }
                else
                {
                    _logger.LogInformation($"Password reset confirmation request was successfully dispatched to {user.UserName}.");
                    return(NoContent());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception thrown during password reset request.");
                return(StatusCode(500, "Password reset request unsuccessful."));
            }
        }