1 //------------------------------------------------------------------------------
2 // <copyright file="ProfileManager.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
9 * Copyright (c) 2002 Microsoft Corporation
12 namespace System.Web.Profile
14 using System.Security.Principal;
15 using System.Security.Permissions;
16 using System.Collections;
17 using System.Collections.Specialized;
18 using System.Web.Configuration;
19 using System.Web.Util;
20 using System.Web.Security;
21 using System.Web.Compilation;
22 using System.Configuration;
23 using System.Configuration.Provider;
24 using System.Reflection;
26 using System.Web.Hosting;
28 public static class ProfileManager
30 private static ProfilePropertySettingsCollection s_dynamicProperties = new ProfilePropertySettingsCollection();
31 internal static ProfilePropertySettingsCollection DynamicProfileProperties {
33 return s_dynamicProperties;
37 public static void AddDynamicProfileProperty(ProfilePropertySettings property) {
38 BuildManager.ThrowIfPreAppStartNotRunning();
39 s_dynamicProperties.Add(property);
42 //////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////
44 //////////////////////////////////////////////////////////////////////
45 public static bool DeleteProfile(string username)
47 SecUtility.CheckParameter( ref username, true, true, true, 0, "username" );
48 return (Provider.DeleteProfiles(new string [] {username}) != 0);
51 //////////////////////////////////////////////////////////////////////
52 //////////////////////////////////////////////////////////////////////
53 //////////////////////////////////////////////////////////////////////
54 public static int DeleteProfiles(ProfileInfoCollection profiles)
56 if( profiles == null )
58 throw new ArgumentNullException( "profiles" );
61 if ( profiles.Count < 1 )
63 throw new ArgumentException(
64 SR.GetString(SR.Parameter_collection_empty,
69 foreach (ProfileInfo pi in profiles) {
70 string username = pi.UserName;
71 SecUtility.CheckParameter(ref username, true, true, true, 0, "UserName");
73 return Provider.DeleteProfiles(profiles);
76 //////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////
78 //////////////////////////////////////////////////////////////////////
79 public static int DeleteProfiles(string[] usernames)
81 SecUtility.CheckArrayParameter( ref usernames,
89 return Provider.DeleteProfiles(usernames);
92 //////////////////////////////////////////////////////////////////////
93 //////////////////////////////////////////////////////////////////////
94 //////////////////////////////////////////////////////////////////////
95 public static int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
97 return Provider.DeleteInactiveProfiles(authenticationOption, userInactiveSinceDate);
100 //////////////////////////////////////////////////////////////////////
101 //////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////
103 public static int GetNumberOfProfiles(ProfileAuthenticationOption authenticationOption)
105 return Provider.GetNumberOfInactiveProfiles(authenticationOption, DateTime.Now.AddDays(1)); //
108 //////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////
111 public static int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
113 return Provider.GetNumberOfInactiveProfiles(authenticationOption, userInactiveSinceDate);
117 //////////////////////////////////////////////////////////////////////
118 //////////////////////////////////////////////////////////////////////
119 //////////////////////////////////////////////////////////////////////
120 public static ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption)
123 return Provider.GetAllProfiles(authenticationOption, 0, Int32.MaxValue, out totalRecords);
127 //////////////////////////////////////////////////////////////////////
128 //////////////////////////////////////////////////////////////////////
129 //////////////////////////////////////////////////////////////////////
130 public static ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption,
133 out int totalRecords)
135 return Provider.GetAllProfiles(authenticationOption, pageIndex, pageSize, out totalRecords);
139 //////////////////////////////////////////////////////////////////////
140 //////////////////////////////////////////////////////////////////////
141 //////////////////////////////////////////////////////////////////////
142 public static ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption,
143 DateTime userInactiveSinceDate)
146 return Provider.GetAllInactiveProfiles(authenticationOption, userInactiveSinceDate, 0, Int32.MaxValue, out totalRecords);
150 //////////////////////////////////////////////////////////////////////
151 //////////////////////////////////////////////////////////////////////
152 //////////////////////////////////////////////////////////////////////
153 public static ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption,
154 DateTime userInactiveSinceDate,
157 out int totalRecords)
159 return Provider.GetAllInactiveProfiles(authenticationOption, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
162 //////////////////////////////////////////////////////////////////////
163 //////////////////////////////////////////////////////////////////////
164 //////////////////////////////////////////////////////////////////////
165 public static ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption,
166 string usernameToMatch)
168 SecUtility.CheckParameter( ref usernameToMatch,
176 return Provider.FindProfilesByUserName(authenticationOption, usernameToMatch, 0, Int32.MaxValue, out totalRecords);
179 //////////////////////////////////////////////////////////////////////
180 //////////////////////////////////////////////////////////////////////
181 //////////////////////////////////////////////////////////////////////
182 public static ProfileInfoCollection FindProfilesByUserName (ProfileAuthenticationOption authenticationOption,
183 string usernameToMatch,
186 out int totalRecords)
190 throw new ArgumentException(SR.GetString(SR.PageIndex_bad), "pageIndex");
195 throw new ArgumentException(SR.GetString(SR.PageSize_bad), "pageSize");
198 SecUtility.CheckParameter( ref usernameToMatch,
205 return Provider.FindProfilesByUserName(authenticationOption, usernameToMatch, pageIndex, pageSize, out totalRecords);
209 //////////////////////////////////////////////////////////////////////
210 //////////////////////////////////////////////////////////////////////
211 //////////////////////////////////////////////////////////////////////
212 public static ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption,
213 string usernameToMatch,
214 DateTime userInactiveSinceDate)
216 SecUtility.CheckParameter( ref usernameToMatch,
224 return Provider.FindInactiveProfilesByUserName(authenticationOption, usernameToMatch, userInactiveSinceDate, 0, Int32.MaxValue, out totalRecords);
227 //////////////////////////////////////////////////////////////////////
228 //////////////////////////////////////////////////////////////////////
229 //////////////////////////////////////////////////////////////////////
230 public static ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption,
231 string usernameToMatch,
232 DateTime userInactiveSinceDate,
235 out int totalRecords)
239 throw new ArgumentException(SR.GetString(SR.PageIndex_bad), "pageIndex");
244 throw new ArgumentException(SR.GetString(SR.PageSize_bad), "pageSize");
247 SecUtility.CheckParameter( ref usernameToMatch,
254 return Provider.FindInactiveProfilesByUserName(authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
259 //////////////////////////////////////////////////////////////////////
260 //////////////////////////////////////////////////////////////////////
261 //////////////////////////////////////////////////////////////////////
264 //////////////////////////////////////////////////////////////////////
265 //////////////////////////////////////////////////////////////////////
266 public static bool Enabled {
269 if (!s_Initialized && !s_InitializedEnabled) {
270 InitializeEnabled(false);
277 //////////////////////////////////////////////////////////////////////
278 //////////////////////////////////////////////////////////////////////
279 public static string ApplicationName {
281 return Provider.ApplicationName;
284 Provider.ApplicationName = value;
288 //////////////////////////////////////////////////////////////////////
289 //////////////////////////////////////////////////////////////////////
290 public static bool AutomaticSaveEnabled {
292 HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
293 // WOS #1544130: Don't initialize providers when getting this property, because it is called in ProfileModule.Init
294 InitializeEnabled(false);
295 return s_AutomaticSaveEnabled;
298 //////////////////////////////////////////////////////////////////////
299 //////////////////////////////////////////////////////////////////////
300 public static ProfileProvider Provider {
302 HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
304 if (s_Provider == null) {
305 throw new InvalidOperationException(SR.GetString(SR.Profile_default_provider_not_found));
311 //////////////////////////////////////////////////////////////////////
312 //////////////////////////////////////////////////////////////////////
313 public static ProfileProviderCollection Providers {
315 HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
321 //////////////////////////////////////////////////////////////////////
322 //////////////////////////////////////////////////////////////////////
323 //////////////////////////////////////////////////////////////////////
326 private static void InitializeEnabled(bool initProviders) {
327 if (!s_Initialized || !s_InitializedProviders || !s_InitializeDefaultProvider) {
329 if (!s_Initialized || !s_InitializedProviders || !s_InitializeDefaultProvider) {
331 ProfileSection config = MTConfigUtil.GetProfileAppConfig();
332 if (!s_InitializedEnabled) {
333 s_Enabled = config.Enabled && HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low);
334 s_AutomaticSaveEnabled = s_Enabled && config.AutomaticSaveEnabled;
335 s_InitializedEnabled = true;
337 if (initProviders && s_Enabled && (!s_InitializedProviders || !s_InitializeDefaultProvider)) {
338 InitProviders(config);
341 catch (Exception e) {
345 s_Initialized = true;
351 private static void Initialize(bool throwIfNotEnabled)
353 InitializeEnabled(true);
354 if (s_InitException != null)
355 throw s_InitException;
356 if (throwIfNotEnabled && !s_Enabled)
357 throw new ProviderException(SR.GetString(SR.Profile_not_enabled));
360 //////////////////////////////////////////////////////////////////////
361 //////////////////////////////////////////////////////////////////////
362 //////////////////////////////////////////////////////////////////////
363 static private void InitProviders(ProfileSection config)
365 if (!s_InitializedProviders) {
366 s_Providers = new ProfileProviderCollection();
367 if (config.Providers != null) {
368 ProvidersHelper.InstantiateProviders(config.Providers, s_Providers, typeof(ProfileProvider));
370 s_InitializedProviders = true;
373 bool canInitializeDefaultProvider = (!HostingEnvironment.IsHosted || BuildManager.PreStartInitStage == PreStartInitStage.AfterPreStartInit);
374 if (!s_InitializeDefaultProvider && canInitializeDefaultProvider) {
375 s_Providers.SetReadOnly();
377 if (config.DefaultProvider == null)
378 throw new ProviderException(SR.GetString(SR.Profile_default_provider_not_specified));
380 s_Provider = (ProfileProvider)s_Providers[config.DefaultProvider];
381 if (s_Provider == null)
382 throw new ConfigurationErrorsException(SR.GetString(SR.Profile_default_provider_not_found), config.ElementInformation.Properties["providers"].Source, config.ElementInformation.Properties["providers"].LineNumber);
384 s_InitializeDefaultProvider = true;
388 //////////////////////////////////////////////////////////////////////
389 //////////////////////////////////////////////////////////////////////
390 //////////////////////////////////////////////////////////////////////
391 private static ProfileProvider s_Provider;
392 private static ProfileProviderCollection s_Providers;
393 private static bool s_Enabled;
394 private static bool s_Initialized;
395 private static bool s_InitializedProviders;
396 private static bool s_InitializeDefaultProvider;
397 private static object s_Lock = new object();
398 private static Exception s_InitException;
399 private static bool s_InitializedEnabled;
400 private static bool s_AutomaticSaveEnabled;