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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
//------------------------------------------------------------------------------
// <copyright file="ProfileService.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.ApplicationServices {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Management;
using System.Web.Profile;
using System.Web.Resources;
[ServiceContract(Namespace="http://asp.net/ApplicationServices/v200")]
[ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(Namespace="http://asp.net/ApplicationServices/v200", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ProfileService {
//////////////////////////////////////////////////////////////////////
/// <devdoc>
/// Raised to allow developers to validate property values being set by the client
/// </devdoc>
private static object _validatingPropertiesEventHandlerLock = new object();
private static EventHandler<ValidatingPropertiesEventArgs> _validatingProperties;
public static event EventHandler<ValidatingPropertiesEventArgs> ValidatingProperties {
add {
lock (_validatingPropertiesEventHandlerLock) {
_validatingProperties += value;
}
}
remove {
lock (_validatingPropertiesEventHandlerLock) {
_validatingProperties -= value;
}
}
}
/// <devdoc>
/// Raises the ValidatingPropertiesEvent if atleast one handler is assigned.
/// </devdoc>
private void OnValidatingProperties(ValidatingPropertiesEventArgs e) {
EventHandler<ValidatingPropertiesEventArgs> handler = _validatingProperties;
if (null != handler) {
handler(this, e);
}
}
public ProfileService() {
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
[OperationContract]
public Dictionary<string, object> GetPropertiesForCurrentUser(IEnumerable<string> properties, bool authenticatedUserOnly) {
if (properties == null) {
throw new ArgumentNullException("properties");
}
ApplicationServiceHelper.EnsureProfileServiceEnabled();
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(HttpContext.Current);
}
Dictionary<string, object> retDict = new Dictionary<string, object>();
ProfileBase pb = null;
try {
pb = GetProfileForCurrentUser(authenticatedUserOnly);
}
catch (Exception e) {
LogException(e);
throw;
}
if (pb == null) {
return null;
}
Dictionary<string, object> allowedGet = ApplicationServiceHelper.ProfileAllowedGet;
if (allowedGet == null || allowedGet.Count == 0) {
// there are no readable properties
return retDict;
}
foreach (string property in properties) {
if (property == null) {
throw new ArgumentNullException("properties");
}
if (allowedGet.ContainsKey(property)) {
try {
SettingsPropertyValue value = GetPropertyValue(pb, property);
if (value != null) {
retDict.Add(property, value.PropertyValue);
value.IsDirty = false;
}
}
catch (Exception e) {
LogException(e);
throw;
}
}
}
return retDict;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
[OperationContract]
public Dictionary<string, object> GetAllPropertiesForCurrentUser(bool authenticatedUserOnly) {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(HttpContext.Current);
}
Dictionary<string, object> retDict = new Dictionary<string, object>();
try {
ProfileBase pb = GetProfileForCurrentUser(authenticatedUserOnly);
if (pb == null) {
return null;
}
Dictionary<string, object> allowedGet = ApplicationServiceHelper.ProfileAllowedGet;
if (allowedGet == null || allowedGet.Count == 0) {
// there are no readable properties
return retDict;
}
foreach (KeyValuePair<string, object> entry in allowedGet) {
string propertyName = entry.Key;
SettingsPropertyValue value = GetPropertyValue(pb, propertyName);
if (value != null) {
retDict.Add(propertyName, value.PropertyValue);
value.IsDirty = false;
}
}
}
catch (Exception e) {
LogException(e);
throw;
}
return retDict;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
[OperationContract]
public Collection<string> SetPropertiesForCurrentUser(IDictionary<string, object> values, bool authenticatedUserOnly) {
if (values == null) {
throw new ArgumentNullException("values");
}
ApplicationServiceHelper.EnsureProfileServiceEnabled();
if (authenticatedUserOnly) {
ApplicationServiceHelper.EnsureAuthenticated(HttpContext.Current);
}
Collection<string> sc = new Collection<string>();
try {
ValidatingPropertiesEventArgs vp = new ValidatingPropertiesEventArgs(values);
OnValidatingProperties(vp);
Dictionary<string, object> allowedSet = ApplicationServiceHelper.ProfileAllowedSet;
ProfileBase pb = GetProfileForCurrentUser(authenticatedUserOnly);
foreach (KeyValuePair<string, object> kvp in values) {
string propertyName = kvp.Key;
if (pb == null) {
sc.Add(propertyName);
continue;
}
if (vp.FailedProperties.Contains(propertyName)) {
sc.Add(propertyName);
continue;
}
if (allowedSet == null) {
sc.Add(propertyName);
continue;
}
if (!allowedSet.ContainsKey(propertyName)) {
sc.Add(propertyName);
continue;
}
SettingsProperty settingProperty = ProfileBase.Properties[propertyName];
if (settingProperty == null) {
// property not found
sc.Add(propertyName);
continue;
}
if (settingProperty.IsReadOnly || (pb.IsAnonymous && !(bool)settingProperty.Attributes["AllowAnonymous"])) {
// property is readonly, or the profile is anonymous and the property isn't enabled for anonymous access
sc.Add(propertyName);
continue;
}
SettingsPropertyValue value = GetPropertyValue(pb, kvp.Key);
if (value == null) { // property not found
sc.Add(propertyName);
continue;
}
else {
try {
pb[propertyName] = kvp.Value;
}
catch (System.Configuration.Provider.ProviderException) {
// provider specific error
sc.Add(propertyName);
}
catch (System.Configuration.SettingsPropertyNotFoundException) {
sc.Add(propertyName);
}
catch (System.Configuration.SettingsPropertyWrongTypeException) {
sc.Add(propertyName);
}
}
}
pb.Save();
}
catch (Exception e) {
LogException(e);
throw;
}
return sc;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
[OperationContract]
public ProfilePropertyMetadata[] GetPropertiesMetadata() {
ApplicationServiceHelper.EnsureProfileServiceEnabled();
try {
// todo: convert to array is temporary -- this method should just return Collection<> like the other profileservice does.
Collection<ProfilePropertyMetadata> metadatas = ApplicationServiceHelper.GetProfilePropertiesMetadata();
ProfilePropertyMetadata[] metadatasArray = new ProfilePropertyMetadata[metadatas.Count];
metadatas.CopyTo(metadatasArray, 0);
return metadatasArray;
}
catch (Exception e) {
LogException(e);
throw;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private static SettingsPropertyValue GetPropertyValue(ProfileBase pb, string name) {
SettingsProperty prop = ProfileBase.Properties[name];
if (prop == null) {
return null;
}
SettingsPropertyValue p = pb.PropertyValues[name];
if (p == null) {
// not fetched from provider
pb.GetPropertyValue(name); // to force a fetch from the provider
p = pb.PropertyValues[name];
if (p == null) {
return null;
}
}
return p;
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private static ProfileBase GetProfileForCurrentUser(bool authenticatedUserOnly) {
HttpContext context = HttpContext.Current;
IPrincipal user = ApplicationServiceHelper.GetCurrentUser(context);
string name = null;
bool isAuthenticated = false;
if (user == null || user.Identity == null || string.IsNullOrEmpty(user.Identity.Name)) { // anonymous user?
isAuthenticated = false;
if (!authenticatedUserOnly && context != null && !string.IsNullOrEmpty(context.Request.AnonymousID)) { // Use Anonymous ID?
name = context.Request.AnonymousID;
}
}
else {
name = user.Identity.Name;
isAuthenticated = user.Identity.IsAuthenticated;
}
if (!isAuthenticated && (authenticatedUserOnly || string.IsNullOrEmpty(name))) {
if (context != null)
throw new HttpException(AtlasWeb.UserIsNotAuthenticated);
else
throw new Exception(AtlasWeb.UserIsNotAuthenticated);
}
return ProfileBase.Create(name, isAuthenticated);
}
private void LogException(Exception e) {
WebServiceErrorEvent errorevent = new WebServiceErrorEvent(AtlasWeb.UnhandledExceptionEventLogMessage, this, e);
errorevent.Raise();
}
}
}
|