public PasswordPolicyVerifierTests() { var factory = new PolicyFactory(); var provider = new PasswordPolicyProvider(factory); _verifier = new PasswordPolicyVerifier(provider); }
/// <summary> /// Registers new user account to database. /// </summary> /// <returns>True if user account is successfully created in the database</returns> /// <remarks>Set all required properties of the user instance and then call this method.</remarks> /// <exception cref="System.ArgumentException">Thrown when any of the required properties of ZentityUser object are not set.</exception> /// <exception cref="Zentity.Security.Authentication.AuthenticationException">Thrown if password does not conform to password policy.</exception> /// <example> /// <code> /// try /// { /// ZentityUser newUser = new ZentityUser(); /// //Set the mandatory properties of the ZentityUser object. /// newUser.FirstName = "John"; /// newUser.Email = "[email protected]"; /// newUser.LogOnName = "JohnDE"; /// newUser.SetPassword("john@123"); //In case of a UI accepting user inputs this call would be newUser.SetPassword(passwordBox1.Password) /// newUser.SetSecurityQuestion("What is a bit?"); /// newUser.SetAnswer("0 or 1"); /// //Optional properties - the user can be registered with or without setting these properties. /// newUser.MiddleName = "D"; /// newUser.LastName = "Erickson"; /// newUser.City = "New York"; /// newUser.State = "New York State"; /// newUser.Country = "USA"; /// /// //Call the Register() method. /// bool newUserRegistered = newUser.Register(); /// if (newUserRegistered) /// { /// Console.WriteLine("User John registered successfully"); /// } /// else /// { /// //false value might mean the logon name is already in use. /// Console.WriteLine(@"User John could not be registered. The logon name chosen might be already in use. /// Try choosing a different logon name."); /// } /// } /// //AuthenticationException might be thrown in case of errors in connecting to the authentication store /// //or if the chosen password does not conform to password policy. /// catch (AuthenticationException ex) /// { /// Console.WriteLine(ex.Message); /// //In case of database errors the AuthenticationException object will wrap the sql exception. /// if (ex.InnerException != null) /// { /// Console.WriteLine(ex.InnerException.Message); /// } /// } /// /// </code> /// </example> public bool Register() { #region Validation // Check whether all required properties are set ValidateParameters( "FirstName", this.Profile.FirstName, "Email", this.Profile.Email, "LogOnName", this.Profile.LogOnName, "Password", this.Password, "SecurityQuestion", this.Profile.SecurityQuestion, "Answer", this.Profile.Answer); string plainTextPassword = PasswordManager.GetPlainPassword(this.Password); if (!PasswordPolicyProvider.CheckPolicyConformance(plainTextPassword)) { throw new AuthenticationException(ConstantStrings.PolicyConformanceExceptionMessage); } #endregion //// Set account status to active this.Profile.AccountStatus = "Active"; //// Save the new user to the database bool success = DataAccessLayer.RegisterUser(this); return success; }
public void GetPolicyTest_ShouldSucceed() { var provider = new PasswordPolicyProvider(); var result = provider.GetPolicy(); Assert.NotNull(result); Assert.True(result.MinimumLength > 0); }
public async Task GetPolicyAsyncTest_ShouldSucceed() { var provider = new PasswordPolicyProvider(); var result = await provider.GetPolicyAsync(); Assert.NotNull(result); Assert.True(result.MinimumLength > 0); }
public void GetPolicyTest_ShouldSucceed() { var factory = new PolicyFactory(); var provider = new PasswordPolicyProvider(factory); var result = provider.GetPolicies(); Assert.NotNull(result); Assert.True(result.Any()); }
/// <summary> /// Changes user password if the current password is verified to be correct /// </summary> /// <param name="newPassword">New password</param> /// <returns>True if user password is changed successfully</returns> /// <remarks>Set logon name, password of the ZentityUser object to correct values and then call this method.</remarks> /// <exception cref="System.ArgumentException">Thrown when this method is called without first setting the /// logon name and password of the ZentityUser object.</exception> /// <exception cref="Zentity.Security.Authentication.AuthenticationException">Thrown when new password does not conform to password policy.</exception> /// <example> /// <code> /// try /// { /// //For changing password create an instance of ZentityUser and set logon name and password. /// //Then call ChangePassword method with new password as the parameter. /// ZentityUser user = new ZentityUser { LogOnName = "john" }; /// user.SetPassword("john@123"); //In case of UI accepting user inputs this call would be something like user.SetPassword(passwordBox1.Password); /// bool isPasswordChanged = user.ChangePassword("john!@#4"); /// if (isPasswordChanged) /// { /// Console.WriteLine("Password changed"); /// } /// else /// { /// Console.WriteLine("Errors while changing password. Please verify whether the logon name and current password are correct."); /// } /// } /// catch (AuthenticationException ex) /// { /// //AuthenticationException may be thrown in case of database errors, or if new password does not conform to password policy. /// Console.WriteLine(ex.Message); /// //In case of database errors the AuthenticationException object will wrap the sql exception. /// if (ex.InnerException != null) /// { /// Console.WriteLine(ex.InnerException.Message); /// } /// } /// /// </code> /// </example> public bool ChangePassword(string newPassword) { #region Validations ValidateParameters("newPassword", newPassword); #endregion if (PasswordPolicyProvider.CheckPolicyConformance(newPassword)) { bool success = PasswordManager.ChangePassword(this.LogOnName, this.Password, newPassword); return success; } else { throw new AuthenticationException(ConstantStrings.PolicyConformanceExceptionMessage); } }