/// <summary> /// Writes the profile settings for the current user to the <see cref="ProfileBase"/> /// using the specified <paramref name="user"/>. /// </summary> /// <remarks> /// The profile is updated from the <paramref name="user"/> using the following algorithm. /// <para> /// For every property in <paramref name="user"/>: /// if (the property can be read and is in the profile) /// then use the property value to set the value in the profile specified by the alias /// </para> /// </remarks> /// <param name="user">The user to update the profile settings with</param> /// <exception cref="InvalidDataContractException"> is thrown if a property in /// <paramref name="user"/> that meets the specified conditions does not have a /// corresponding profile value. /// </exception> private static void UpdateProfile(T user) { if (string.IsNullOrEmpty(user.Name) || !ProfileManager.Enabled) { return; } // We're using the current Profile since we've verified it matches the current // principal and it allows us to leverage the auto-save feature. When testing, // however, we'll need to create a new one. ProfileBase profile = AuthenticationBase <T> .GetProfileBase(user.Name); foreach (PropertyInfo property in user.GetType().GetProperties()) { if (!property.CanRead || !property.CanWrite || !AuthenticationBase <T> .IsInProfile(property) || AuthenticationBase <T> .IsReadOnly(property) || property.GetIndexParameters().Length > 0) { // Skip this property if it is not readable, in the profile, is readonly or is an indexer property continue; } try { profile.SetPropertyValue(AuthenticationBase <T> .GetProfileAlias(property), property.GetValue(user, null)); } catch (SettingsPropertyNotFoundException e) { throw new InvalidDataContractException( string.Format( CultureInfo.InvariantCulture, Resources.ApplicationServices_ProfilePropertyDoesNotExist, AuthenticationBase <T> .GetProfileAlias(property)), e); } catch (SettingsPropertyIsReadOnlyException e) { throw new InvalidDataContractException( string.Format( CultureInfo.InvariantCulture, Resources.ApplicationServices_ProfilePropertyReadOnly, AuthenticationBase <T> .GetProfileAlias(property)), e); } catch (SettingsPropertyWrongTypeException e) { throw new InvalidDataContractException( string.Format( CultureInfo.InvariantCulture, Resources.ApplicationServices_ProfilePropertyTypeMismatch, AuthenticationBase <T> .GetProfileAlias(property)), e); } } // Explicit invocation is necessary when auto-save is not enabled bool isAutoSaveEnabled = false; try { isAutoSaveEnabled = ProfileManager.AutomaticSaveEnabled; } catch (HttpException) { // If the feature is not supported at the current hosting permission level, // we can assume it is not enabled. } if (!isAutoSaveEnabled) { profile.Save(); } }