private void ChangeProfileProperty(ProfilePropertyDataStore propStore, ProfileUser user, SettingsPropertyValue propValue)
        {
            ProfileProperty dbProperty = propStore.FindByPropertyName(user, propValue.Name);
            if (dbProperty == null) //Create the property if not found
            {
                dbProperty = new ProfileProperty(user, propValue.Name);
                propStore.Insert(dbProperty);
            }

            //The property is already deserialized and is null
            if (propValue.Deserialized && propValue.PropertyValue == null)
                dbProperty.SetNull();
            else //Property is not null
            {
                object serializedVal = propValue.SerializedValue;

                if (serializedVal == null) //null
                    dbProperty.SetNull();
                else if (serializedVal is string) //string
                    dbProperty.SetValue((string)serializedVal);
                else if (serializedVal is byte[]) //binary
                    dbProperty.SetValue((byte[])serializedVal);
                else
                    throw new ProfileValueNotSupportedException(propValue.Name);
            }
        }
        private IList<ProfileProperty> GetProperties(string userName)
        {
            if (string.IsNullOrEmpty(userName))
                return null;

            IList<ProfileProperty> properties;
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ProfileUserDataStore userStore = new ProfileUserDataStore(transaction);
                ProfileUser user = userStore.FindByName(ApplicationName, userName);
                if (user == null)
                    return null;

                ProfilePropertyDataStore propStore = new ProfilePropertyDataStore(transaction);

                properties = propStore.FindByUser(user);

                //Update the last activity date
                user.LastActivityDate = DateTime.Now;

                transaction.Commit();
            }

            return properties;
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection properties)
        {
            string username = (string)context[CONTEXT_USERNAME];
            ProfileType profileType;
            if ((bool)context[CONTEXT_ISAUTHENTICATED])
                profileType = ProfileType.Authenticated;
            else
                profileType = ProfileType.Anonymous;

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ProfilePropertyDataStore propStore = new ProfilePropertyDataStore(transaction);
                ProfileUserDataStore userStore = new ProfileUserDataStore(transaction);

                ProfileUser user = userStore.FindByName(ApplicationName, username);
                //Create the user if not exist
                if (user == null)
                {
                    user = new ProfileUser(ApplicationName, username, profileType);
                    userStore.Insert(user);
                }

                bool userChanged = false;

                foreach (SettingsPropertyValue propValue in properties)
                {
                    if (propValue.IsDirty)
                    {
                        if (profileType == ProfileType.Anonymous)
                        {
                            bool allowAnonymous = (bool)propValue.Property.Attributes[PROP_ATTRIBUTE_ALLOWANONYMOUS];
                            if (!allowAnonymous)
                                continue;
                        }

                        userChanged = true;

                        ChangeProfileProperty(propStore, user, propValue);
                    }
                }

                user.LastActivityDate = DateTime.Now;
                if (userChanged)
                    user.LastPropertyChangedDate = DateTime.Now;

                transaction.Commit();
            }
        }