1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
//------------------------------------------------------------------------------
// <copyright file="ProfileServiceManager.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Web.ApplicationServices;
using System.Web;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Resources;
using System.Web.Script.Serialization;
using System.Web.Configuration;
[
DefaultProperty("Path"),
TypeConverter(typeof(EmptyStringExpandableObjectConverter))
]
public class ProfileServiceManager {
private string[] _loadProperties;
private string _path;
internal static void ConfigureProfileService(ref StringBuilder sb, HttpContext context, ScriptManager scriptManager, List<ScriptManagerProxy> proxies) {
string profileServiceUrl = null;
ArrayList loadedProperties = null;
ProfileServiceManager profileManager;
if(scriptManager.HasProfileServiceManager) {
profileManager = scriptManager.ProfileService;
// get ScriptManager.Path
profileServiceUrl = profileManager.Path.Trim();
if(profileServiceUrl.Length > 0) {
profileServiceUrl = scriptManager.ResolveClientUrl(profileServiceUrl);
}
// get ScriptManager.LoadProperties
if(profileManager.HasLoadProperties) {
loadedProperties = new ArrayList(profileManager._loadProperties);
}
}
// combine proxy Paths (find the first one that has specified one)
// combine loadedProperties collection (take the union of all)
if(proxies != null) {
foreach(ScriptManagerProxy proxy in proxies) {
if(proxy.HasProfileServiceManager) {
profileManager = proxy.ProfileService;
// combine urls
profileServiceUrl = ApplicationServiceManager.MergeServiceUrls(profileManager.Path, profileServiceUrl, proxy);
// combine LoadProperties
if(profileManager.HasLoadProperties) {
if(loadedProperties == null) {
loadedProperties = new ArrayList(profileManager._loadProperties);
}
else {
loadedProperties = ProfileServiceManager.MergeProperties(loadedProperties, profileManager._loadProperties);
}
}
}
}
}
ProfileServiceManager.GenerateInitializationScript(ref sb, context, scriptManager, profileServiceUrl, loadedProperties);
}
private static void GenerateInitializationScript(ref StringBuilder sb, HttpContext context, ScriptManager scriptManager, string serviceUrl, ArrayList loadedProperties) {
string defaultServicePath = null;
bool loadProperties = loadedProperties != null && loadedProperties.Count > 0;
if (ApplicationServiceHelper.ProfileServiceEnabled) {
if (sb == null) {
sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
}
// The default path points to the built-in service (if it is enabled)
// Note that the client can't default to this path because it doesn't know what the app root is, we must tell it.
// We must specify the default path to the proxy even if a custom path is provided, because on the client they could
// reset the path back to the default if they want.
defaultServicePath = scriptManager.ResolveClientUrl("~/" + System.Web.Script.Services.WebServiceData._profileServiceFileName);
sb.Append("Sys.Services._ProfileService.DefaultWebServicePath = '");
sb.Append(HttpUtility.JavaScriptStringEncode(defaultServicePath));
sb.Append("';\n");
}
if (!String.IsNullOrEmpty(serviceUrl)) {
// DevDiv Bug 72257:When custom path is set and loadProperties=True, we shouldn't use the default path
// loadProperties script always retrieves the properties from default profile provider, which is not correct if ProfileService
// points to non default path. Hence throw when non default path and loadProperties both are specified.
if (defaultServicePath == null){
defaultServicePath = scriptManager.ResolveClientUrl("~/" + System.Web.Script.Services.WebServiceData._profileServiceFileName);
}
if (loadProperties && !String.Equals(serviceUrl, defaultServicePath, StringComparison.OrdinalIgnoreCase)) {
throw new InvalidOperationException(AtlasWeb.ProfileServiceManager_LoadProperitesWithNonDefaultPath);
}
if (sb == null) {
sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
}
sb.Append("Sys.Services.ProfileService.set_path('");
sb.Append(HttpUtility.JavaScriptStringEncode(serviceUrl));
sb.Append("');\n");
}
if (loadProperties) {
if (sb == null) {
sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
}
if (scriptManager.DesignMode) {
// Dev10 757178: context is null at design time, so we cannot access ProfileBase.
// But at DesignTime this method is only important because if it produces any init script,
// it prompts AddFrameworkScripts to add the MicrosoftAjaxApplicationServices.js script reference.
// So just append a comment to ensure at least some script is generated.
sb.Append("// loadProperties\n");
}
else if (context != null) {
// get values for all properties to be pre-loaded.
// GetSettingsProperty puts each property into either the top level settings dictionary or if its part of a group,
// it creates an entry for the group in the group collection and puts the setting in the dictionary for the group.
SortedList<string, object> topLevelSettings = new SortedList<string, object>(loadedProperties.Count);
SortedList<string, SortedList<string, object>> profileGroups = null;
ProfileBase profile = context.Profile;
foreach(string propertyFullName in loadedProperties) {
GetSettingsProperty(profile, propertyFullName, topLevelSettings, ref profileGroups, /* ensure exists */ true);
}
RenderProfileProperties(sb, topLevelSettings, profileGroups);
}
}
}
internal static ArrayList MergeProperties(ArrayList existingProperties, string[] newProperties) {
//
foreach(string property in newProperties) {
if(!String.IsNullOrEmpty(property)) {
string trimmedProperty = property.Trim();
if((trimmedProperty.Length > 0) && !existingProperties.Contains(trimmedProperty)) {
existingProperties.Add(trimmedProperty);
}
}
}
return existingProperties;
}
internal static void GetSettingsProperty(
ProfileBase profile,
string fullPropertyName,
SortedList<string, object> topLevelSettings,
ref SortedList<string, SortedList<string, object>> profileGroups,
bool ensureExists) {
// Gets a setting off the profile, putting top level settings into the topLevelSettings list,
// and putting grouped properties into a group-specific list that is contained within a sortedlist of groups.
// if ensureExists is true and the given property name doesn't exist on the profile, an exception is thrown.
int dotIndex = fullPropertyName.IndexOf('.');
string groupName;
string propertyName;
SortedList<string, object> containingObject;
if(dotIndex == -1) {
groupName = null;
propertyName = fullPropertyName;
containingObject = topLevelSettings;
}
else {
groupName = fullPropertyName.Substring(0, dotIndex);
propertyName = fullPropertyName.Substring(dotIndex + 1);
if(profileGroups == null) {
profileGroups = new SortedList<string, SortedList<string, object>>();
containingObject = new SortedList<string, object>();
profileGroups.Add(groupName, containingObject);
}
else {
containingObject = profileGroups[groupName];
if(containingObject == null) {
containingObject = new SortedList<string, object>();
profileGroups.Add(groupName, containingObject);
}
}
}
bool exists = ProfileBase.Properties[fullPropertyName] != null;
if(ensureExists && !exists) {
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, AtlasWeb.AppService_UnknownProfileProperty, fullPropertyName));
}
if(exists) {
containingObject[propertyName] = profile == null ? null : profile[fullPropertyName];
}
}
private static void RenderProfileProperties(StringBuilder sb, SortedList<string, object> topLevelSettings, SortedList<string, SortedList<string, object>> profileGroups) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
// 1. render top level settings
sb.Append("Sys.Services.ProfileService.properties = ");
//
sb.Append(serializer.Serialize(topLevelSettings, JavaScriptSerializer.SerializationFormat.JavaScript));
sb.Append(";\n");
// 2. render each group as a ProfileGroup object
// These could be done as just the value of the PropertyName in topLevelSettings but the serializer wouldn't recognize PropertyGroup.
if(profileGroups != null) {
foreach(KeyValuePair<string, SortedList<string, object>> group in profileGroups) {
sb.Append("Sys.Services.ProfileService.properties.");
sb.Append(group.Key); // group name
sb.Append(" = new Sys.Services.ProfileGroup(");
sb.Append(serializer.Serialize(group.Value, JavaScriptSerializer.SerializationFormat.JavaScript));
sb.Append(");\n");
}
}
}
internal bool HasLoadProperties {
get {
return _loadProperties != null && _loadProperties.Length > 0;
}
}
[
DefaultValue(null),
Category("Behavior"),
NotifyParentProperty(true),
TypeConverter(typeof(StringArrayConverter)),
ResourceDescription("ProfileServiceManager_LoadProperties"),
SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays",
Justification="Required by ASP.NET parser.")
]
public string[] LoadProperties {
get {
if(_loadProperties == null) {
_loadProperties = new string[0];
}
return (string[]) _loadProperties.Clone();
}
set {
if(value != null) {
value = (string[])value.Clone();
}
_loadProperties = value;
}
}
[
DefaultValue(""),
Category("Behavior"),
NotifyParentProperty(true),
ResourceDescription("ApplicationServiceManager_Path"),
UrlProperty()
]
public string Path {
get {
return _path ?? String.Empty;
}
set {
_path = value;
}
}
}
}
|