Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / xsp / System / Web / Extensions / Profile / ProfileService.cs / 1 / ProfileService.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.Profile {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Web.ApplicationServices;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
[ScriptService]
internal sealed class ProfileService {
private static JavaScriptSerializer _javaScriptSerializer;
private static JavaScriptSerializer JavaScriptSerializer {
get {
if (_javaScriptSerializer == null) {
HttpContext context = HttpContext.Current;
WebServiceData webServiceData = WebServiceData.GetWebServiceData(context, context.Request.FilePath);
_javaScriptSerializer = webServiceData.Serializer;
Debug.Assert(_javaScriptSerializer != null);
}
return _javaScriptSerializer;
}
}
private static Dictionary GetProfile(HttpContext context, IEnumerable properties) {
ProfileBase profile = context.Profile;
if(profile == null) {
return null;
}
Dictionary allowedGet = ApplicationServiceHelper.ProfileAllowedGet;
if (allowedGet == null || allowedGet.Count == 0) {
// there are no readable properties
return new Dictionary(0);
}
Dictionary dictionary = null;
if(properties == null) {
// initialize capacity to the exact number we will fill: the number of readAccessProperties
dictionary = new Dictionary(allowedGet.Count, StringComparer.OrdinalIgnoreCase);
// Returns all profile properties defined in configuration when given properties array is null
string propertyName;
foreach(KeyValuePair entry in allowedGet) {
// note: dont enumerate over _allowedGet.Keys since it unecessarily creates a keys collection object
propertyName = entry.Key;
dictionary.Add(propertyName, profile[propertyName]);
}
}
else {
// initialize capacity to the largest possible number of properties we may return.
dictionary = new Dictionary(allowedGet.Count, StringComparer.OrdinalIgnoreCase);
// Returns the specified profile properties (if empty array, no properties returned)
foreach(string propertyName in properties) {
if(allowedGet.ContainsKey(propertyName)) {
dictionary.Add(propertyName, profile[propertyName]);
}
}
}
return dictionary;
}
private static Collection SetProfile(HttpContext context, IDictionary values) {
// return collection of successfully saved settings.
Collection failedSettings = new Collection();
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 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 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;
}
[WebMethod]
public Dictionary GetAllPropertiesForCurrentUser(bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.GetProfile(context, null);
}
[WebMethod]
public Dictionary GetPropertiesForCurrentUser(IEnumerable properties, bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.GetProfile(context, properties);
}
[WebMethod]
public Collection GetPropertiesMetadata() {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
return ApplicationServiceHelper.GetProfilePropertiesMetadata();
}
[WebMethod]
public Collection SetPropertiesForCurrentUser(IDictionary values, bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.SetProfile(context, values);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.Profile {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Web.ApplicationServices;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
[ScriptService]
internal sealed class ProfileService {
private static JavaScriptSerializer _javaScriptSerializer;
private static JavaScriptSerializer JavaScriptSerializer {
get {
if (_javaScriptSerializer == null) {
HttpContext context = HttpContext.Current;
WebServiceData webServiceData = WebServiceData.GetWebServiceData(context, context.Request.FilePath);
_javaScriptSerializer = webServiceData.Serializer;
Debug.Assert(_javaScriptSerializer != null);
}
return _javaScriptSerializer;
}
}
private static Dictionary GetProfile(HttpContext context, IEnumerable properties) {
ProfileBase profile = context.Profile;
if(profile == null) {
return null;
}
Dictionary allowedGet = ApplicationServiceHelper.ProfileAllowedGet;
if (allowedGet == null || allowedGet.Count == 0) {
// there are no readable properties
return new Dictionary(0);
}
Dictionary dictionary = null;
if(properties == null) {
// initialize capacity to the exact number we will fill: the number of readAccessProperties
dictionary = new Dictionary(allowedGet.Count, StringComparer.OrdinalIgnoreCase);
// Returns all profile properties defined in configuration when given properties array is null
string propertyName;
foreach(KeyValuePair entry in allowedGet) {
// note: dont enumerate over _allowedGet.Keys since it unecessarily creates a keys collection object
propertyName = entry.Key;
dictionary.Add(propertyName, profile[propertyName]);
}
}
else {
// initialize capacity to the largest possible number of properties we may return.
dictionary = new Dictionary(allowedGet.Count, StringComparer.OrdinalIgnoreCase);
// Returns the specified profile properties (if empty array, no properties returned)
foreach(string propertyName in properties) {
if(allowedGet.ContainsKey(propertyName)) {
dictionary.Add(propertyName, profile[propertyName]);
}
}
}
return dictionary;
}
private static Collection SetProfile(HttpContext context, IDictionary values) {
// return collection of successfully saved settings.
Collection failedSettings = new Collection();
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 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 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;
}
[WebMethod]
public Dictionary GetAllPropertiesForCurrentUser(bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.GetProfile(context, null);
}
[WebMethod]
public Dictionary GetPropertiesForCurrentUser(IEnumerable properties, bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.GetProfile(context, properties);
}
[WebMethod]
public Collection GetPropertiesMetadata() {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
return ApplicationServiceHelper.GetProfilePropertiesMetadata();
}
[WebMethod]
public Collection SetPropertiesForCurrentUser(IDictionary values, bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
HttpContext context = HttpContext.Current;
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(context);
}
return ProfileService.SetProfile(context, values);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- X509Utils.cs
- DropSource.cs
- Synchronization.cs
- RangeBase.cs
- PriorityBindingExpression.cs
- RegexCapture.cs
- TdsRecordBufferSetter.cs
- ToolStripContentPanel.cs
- TreeViewItem.cs
- NavigationProgressEventArgs.cs
- HostUtils.cs
- HMACSHA256.cs
- UidManager.cs
- ToolStripDropTargetManager.cs
- CanExecuteRoutedEventArgs.cs
- ProfileSettingsCollection.cs
- DbConnectionClosed.cs
- WebPartVerbsEventArgs.cs
- TextSegment.cs
- SupportsEventValidationAttribute.cs
- DataContext.cs
- QuaternionValueSerializer.cs
- ListItem.cs
- WorkerProcess.cs
- TextEditorSpelling.cs
- ProcessThreadCollection.cs
- RuntimeHandles.cs
- CallbackValidatorAttribute.cs
- FlowLayout.cs
- TreeViewImageKeyConverter.cs
- BitmapSizeOptions.cs
- ConfigXmlSignificantWhitespace.cs
- ErrorTableItemStyle.cs
- BidOverLoads.cs
- SamlAdvice.cs
- DocumentCollection.cs
- CommandHelpers.cs
- _Win32.cs
- ColumnHeaderConverter.cs
- ViewUtilities.cs
- ViewBox.cs
- FixedSOMPageConstructor.cs
- PrtCap_Base.cs
- SimpleTextLine.cs
- NavigationProgressEventArgs.cs
- ImageBrush.cs
- MtomMessageEncoder.cs
- MDIWindowDialog.cs
- BoolExpr.cs
- WebBodyFormatMessageProperty.cs
- IxmlLineInfo.cs
- SecurityState.cs
- Rect3DValueSerializer.cs
- EntityContainerAssociationSetEnd.cs
- IndicShape.cs
- DataReaderContainer.cs
- WindowsTab.cs
- SecurityUniqueId.cs
- FixedBufferAttribute.cs
- TypeExtension.cs
- FormView.cs
- QilStrConcat.cs
- Pair.cs
- SqlNodeTypeOperators.cs
- WindowsListViewGroup.cs
- CodeFieldReferenceExpression.cs
- ToolstripProfessionalRenderer.cs
- MetadataLocation.cs
- MediaTimeline.cs
- ListViewPagedDataSource.cs
- DesignerTransactionCloseEvent.cs
- NavigationService.cs
- Package.cs
- HtmlButton.cs
- SimpleApplicationHost.cs
- StrongNameUtility.cs
- XD.cs
- Cursors.cs
- DurationConverter.cs
- CngUIPolicy.cs
- StronglyTypedResourceBuilder.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- NamespaceInfo.cs
- HelpInfo.cs
- CounterCreationDataCollection.cs
- XmlILModule.cs
- ProtectedProviderSettings.cs
- CompModSwitches.cs
- TrackingMemoryStreamFactory.cs
- MappingModelBuildProvider.cs
- BindingsSection.cs
- EmissiveMaterial.cs
- Psha1DerivedKeyGeneratorHelper.cs
- AttributeQuery.cs
- FreezableCollection.cs
- ByteAnimationBase.cs
- IISUnsafeMethods.cs
- CopyNodeSetAction.cs
- ValidationPropertyAttribute.cs
- BamlCollectionHolder.cs