コード例 #1
0
        public void TestGetDefaultPasswordPolicy()
        {
            PasswordPolicy passwordPolicy = PasswordPolicyHelper.GetDefaultPasswordPolicy();

            Assert.IsNotNull(passwordPolicy);
        }
コード例 #2
0
        /// <summary>
        ///     Called before saving the enumeration of entities.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        ///     True to cancel the save operation; false otherwise.
        /// </returns>
        public bool OnBeforeSave(IEnumerable <IEntity> entities, IDictionary <string, object> state)
        {
            long passwordFieldId = Entity.GetId("core:password");

            IList <IEntity> enumerable = entities as IList <IEntity> ?? entities.ToList( );

            foreach (IEntity entity in enumerable)
            {
                var userAccount = entity.As <UserAccount>( );
                if (userAccount == null)
                {
                    continue;
                }

                EntityFieldCache.Instance.Get(0);

                _auditLogEventTarget.GatherAuditLogEntityDetailsForSave(userAccount, state);

                var writableCacheKey = new EntityFieldModificationCache.EntityFieldModificationCacheKey((( IEntityInternal )entity.Entity).ModificationToken);

                IEntityFieldValues cachedFieldValues;

                if (EntityFieldModificationCache.Instance.TryGetValue(writableCacheKey, out cachedFieldValues))
                {
                    object newPassword;

                    if (cachedFieldValues.TryGetValue(passwordFieldId, out newPassword))
                    {
                        string password = newPassword as string;

                        var userAccountInternal = userAccount as IEntityInternal;
                        var savedUserAccount    = Entity.Get <UserAccount>(userAccount.Id);

                        if (!userAccountInternal.IsTemporaryId &&
                            password == savedUserAccount.Password)
                        {
                            // Password is unchanged
                            continue;
                        }

                        // Validate the password against the password policy
                        PasswordPolicyHelper.ValidatePassword(PasswordPolicyHelper.GetDefaultPasswordPolicy(), password);

                        // Hash the password before saving
                        userAccount.Password = CryptoHelper.CreateEncodedSaltedHash(password);
                        // The password field was modified, so set the last password change date.
                        userAccount.PasswordLastChanged = DateTime.UtcNow;
                    }
                }

                if (HasUserAccountStatusChanged(userAccount))
                {
                    if (userAccount.AccountStatus_Enum == UserAccountStatusEnum_Enumeration.Active &&
                        userAccount.BadLogonCount > 0)
                    {
                        // Reset the bad logon account when the account is made active again.
                        userAccount.BadLogonCount = 0;
                    }
                }
            }

            return(false);
        }