public PasswordPolicyVerifierTests()
        {
            var factory  = new PolicyFactory();
            var provider = new PasswordPolicyProvider(factory);

            _verifier = new PasswordPolicyVerifier(provider);
        }
示例#2
0
        /// <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 = &quot;John&quot;;
        ///        newUser.Email = &quot;[email protected]&quot;;
        ///        newUser.LogOnName = &quot;JohnDE&quot;;
        ///        newUser.SetPassword(&quot;john@123&quot;); //In case of a UI accepting user inputs this call would be newUser.SetPassword(passwordBox1.Password)
        ///        newUser.SetSecurityQuestion(&quot;What is a bit?&quot;);
        ///        newUser.SetAnswer(&quot;0 or 1&quot;);
        ///        //Optional properties - the user can be registered with or without setting these properties.
        ///        newUser.MiddleName = &quot;D&quot;;
        ///        newUser.LastName = &quot;Erickson&quot;;
        ///        newUser.City = &quot;New York&quot;;
        ///        newUser.State = &quot;New York State&quot;;
        ///        newUser.Country = &quot;USA&quot;;
        ///
        ///        //Call the Register() method.
        ///        bool newUserRegistered = newUser.Register();
        ///        if (newUserRegistered)
        ///        {
        ///            Console.WriteLine(&quot;User John registered successfully&quot;);
        ///        }
        ///        else
        ///        {
        ///            //false value might mean the logon name is already in use.
        ///            Console.WriteLine(@&quot;User John could not be registered. The logon name chosen might be already in use. 
        /// Try choosing a different logon name.&quot;);
        ///        }
        ///    }
        ///    //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;
        }
示例#3
0
        public void GetPolicyTest_ShouldSucceed()
        {
            var provider = new PasswordPolicyProvider();
            var result   = provider.GetPolicy();

            Assert.NotNull(result);
            Assert.True(result.MinimumLength > 0);
        }
示例#4
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());
        }
示例#6
0
        /// <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 = &quot;john&quot; };
        ///        user.SetPassword(&quot;john@123&quot;); //In case of UI accepting user inputs this call would be something like user.SetPassword(passwordBox1.Password);
        ///        bool isPasswordChanged = user.ChangePassword(&quot;john!@#4&quot;);
        ///        if (isPasswordChanged)
        ///        {
        ///            Console.WriteLine(&quot;Password changed&quot;);
        ///        }
        ///        else
        ///        {
        ///            Console.WriteLine(&quot;Errors while changing password. Please verify whether the logon name and current password are correct.&quot;);
        ///        }
        ///    }
        ///    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);
            }
        }