private static Collection <string> SetProfile(HttpContext context, IDictionary <string, object> values)
{
// return collection of successfully saved settings.
Collection <string> failedSettings = new Collection <string>();
if (values == null || values.Count == 0)
{
// no values were given, so we're done, and there are no failures.
return(failedSettings);
}
ProfileBase profile = context.Profile;
Dictionary <string, object> allowedSet = ApplicationServiceHelper.ProfileAllowedSet;
// Note that profile may be null, and allowedSet may be null.
// Even though no properties will be saved in these cases, we still iterate over the given values to be set,
// because we must build up the failed collection anyway.
bool profileDirty = false;
foreach (KeyValuePair <string, object> entry in values)
{
string propertyName = entry.Key;
if (profile != null && allowedSet != null && allowedSet.ContainsKey(propertyName))
{
SettingsProperty settingProperty = ProfileBase.Properties[propertyName];
if (settingProperty != null && !settingProperty.IsReadOnly &&
(!profile.IsAnonymous || (bool)settingProperty.Attributes["AllowAnonymous"]))
{
Type propertyType = settingProperty.PropertyType;
object convertedValue;
if (ObjectConverter.TryConvertObjectToType(entry.Value, propertyType, JavaScriptSerializer, out convertedValue))
{
profile[propertyName] = convertedValue;
profileDirty = true;
// setting successfully saved.
// short circuit the foreach so only failed cases fall through.
continue;
}
}
}
// Failed cases fall through to here. Possible failure reasons:
// 1. type couldn't be converted for some reason (TryConvert returns false)
// 2. the property doesnt exist (settingProperty == null)
// 3. the property is read only (settingProperty.IsReadOnly)
// 4. the current user is anonymous and the setting doesn't allow anonymous access
// 5. profile for this user is null (profile == null)
// 6. no properties are allowed for setting (allowedSet is null)
// 7. *this* property is not allowed for setting (allowedSet.Contains returns false)
failedSettings.Add(propertyName);
}
if (profileDirty)
{
profile.Save();
}
return(failedSettings);
}