Exemple #1
0
        public void InvalidWhenPasswordIsEqualToUsername()
        {
            // Act
            string errorMessage;
            bool   valid = PasswordComplexity.IsValid("user01", "user01", out errorMessage);

            // Assert
            Assert.IsFalse(valid);
            Assert.AreEqual(DefaultErrorMessages.InvalidPasswordEqualToUserName, errorMessage);
        }
Exemple #2
0
        public void ValidWhenPasswordIsEqualToUsername()
        {
            // Act
            string errorMessage;
            bool   valid = PasswordComplexity.IsValid("psswrd", "user01", out errorMessage);

            // Assert
            Assert.IsTrue(valid);
            Assert.IsNull(errorMessage);
        }
        public string CheckPasswordValidity(string userName, string password)
        {
            string errorMessage;

            if (String.IsNullOrWhiteSpace(userName))
            {
                userName = _webSecurity.CurrentUserName;
            }
            PasswordComplexity.IsValid(password, userName, out errorMessage);
            return(errorMessage);
        }
Exemple #4
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    if (_volunteerSvc.EmailAlreadyInUse(model.Email))
                    {
                        throw new PersonAlreadyExistsException();
                    }

                    string errorMessage;
                    if (PasswordComplexity.IsValid(model.Password, model.UserName, out errorMessage))
                    {
                        int    userId;
                        string token     = _webSecurity.CreateUser(model.UserName, model.Password, new[] { Constants.RoleVolunteer }, out userId);
                        var    volunteer = _volunteerSvc.Register(model.FirstName, model.LastName, model.SelectedOrganizationId, model.Email, model.PhoneNumber, userId);
                        if (volunteer != null)
                        {
                            // Generate the absolute Url for the account activation action.
                            var routeValues = new RouteValueDictionary {
                                { "token", token }
                            };
                            var accountActivationLink = Url.Action("ConfirmAccount", "Account", routeValues, Request.Url.Scheme);

                            var body    = String.Format(@"<p>Click on the following link to activate your account: <a href='{0}'>{0}</a></p>", accountActivationLink);
                            var message = new Message("CrisisCheckin - Activate your account", body);

                            _messageService.SendMessage(message, volunteer);
                        }

                        return(RedirectToAction("RegistrationSuccessful", "Account"));
                    }
                    ModelState.AddModelError("Password", errorMessage ?? DefaultErrorMessages.InvalidPasswordFormat);
                }
                catch (PersonAlreadyExistsException)
                {
                    ModelState.AddModelError("Email", "Email is already in use!");
                }
                catch (UserCreationException e)
                {
                    ModelState.AddModelError("", e.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            model.Organizations = _organizationService.GetActiveList();
            return(View(model));
        }
        public override bool IsValid(object value)
        {
            var stringValue = (string)value;

            // Some fields are not required
            if (string.IsNullOrWhiteSpace(stringValue))
            {
                return(true);
            }

            string errorMessage;

            if (!PasswordComplexity.IsValid(stringValue, out errorMessage))
            {
                ErrorMessage = errorMessage;
                return(false);
            }

            return(true);
        }
 public ActionResult ChangePassword(ChangePasswordViewModel model)
 {
     if (ModelState.IsValid)
     {
         if (_webSecurity.ValidateUser(_webSecurity.CurrentUserName, model.OldPassword))
         {
             string errorMessage;
             if (PasswordComplexity.IsValid(model.NewPassword, _webSecurity.CurrentUserName, out errorMessage))
             {
                 _webSecurity.ChangePassword(_webSecurity.CurrentUserName, model.OldPassword, model.NewPassword);
                 return(RedirectToAction("PasswordChanged"));
             }
             ModelState.AddModelError("NewPassword", errorMessage ?? DefaultErrorMessages.InvalidPasswordFormat);
         }
         else
         {
             ModelState.AddModelError("OldPassword", "Old password is not correct.");
         }
     }
     return(View("ChangePassword", DetermineLayout(), null));
 }