private static void InitProviders(ProfileSection config) { if (!s_InitializedProviders) { s_Providers = new ProfileProviderCollection(); if (config.Providers != null) { ProvidersHelper.InstantiateProviders(config.Providers, s_Providers, typeof(ProfileProvider)); } s_InitializedProviders = true; } bool flag = !HostingEnvironment.IsHosted || (BuildManager.PreStartInitStage == PreStartInitStage.AfterPreStartInit); if (!s_InitializeDefaultProvider && flag) { s_Providers.SetReadOnly(); if (config.DefaultProvider == null) { throw new ProviderException(System.Web.SR.GetString("Profile_default_provider_not_specified")); } s_Provider = s_Providers[config.DefaultProvider]; if (s_Provider == null) { throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_default_provider_not_found"), config.ElementInformation.Properties["providers"].Source, config.ElementInformation.Properties["providers"].LineNumber); } s_InitializeDefaultProvider = true; } }
///<summary> ///Initializes the provider. ///</summary> /// ///<param name="config">A collection of the name/value pairs representing the provider-specific /// attributes specified in the configuration for this provider. /// The <c>providerId</c> attribute may be used to override the name being used for looking up an object definition. /// </param> ///<param name="name">The friendly name of the provider.</param> ///<exception cref="T:System.ArgumentNullException">The <paramref name="name"/> or <paramref name="config"/> is null.</exception> ///<exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"></see> on a provider after the provider has already been initialized.</exception> ///<exception cref="T:System.ArgumentException">The <paramref name="name"/> has a length of zero or providerId attribute is not set.</exception> public override void Initialize(string name, NameValueCollection config) { lock (this) { if (config == null) throw new ArgumentNullException("config"); string providerId = config["providerId"]; if (String.IsNullOrEmpty(providerId)) providerId = name; config.Remove("providerId"); this.wrappedProvider = (ProfileProvider)WebApplicationContext.GetRootContext().GetObject(providerId); this.wrappedProvider.Initialize(name, config); } }
private ProfileProvider() { _Provider = ProfileManager.Providers["SqlProfileProvider"]; }
////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// static private void InitializeStatic() { if (!ProfileManager.Enabled || s_Initialized) { if (s_InitializeException != null) { throw s_InitializeException; } return; } lock (s_InitializeLock) { if (s_Initialized) { if (s_InitializeException != null) { throw s_InitializeException; } return; } try { ProfileSection config = MTConfigUtil.GetProfileAppConfig(); bool fAnonEnabled = (HostingEnvironment.IsHosted ? AnonymousIdentificationModule.Enabled : true); Type baseType = ProfileBase.InheritsFromType; bool hasLowTrust = HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low); s_Properties = new SettingsPropertyCollection(); // Step 0: Add all dynamic profile properties set programatically during PreAppStart ProfileBase.AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, ProfileManager.DynamicProfileProperties, null); ////////////////////////////////////////////////////////////////////// // Step 1: Add Properties from the base class (if not ProfileBase) if (baseType != typeof(ProfileBase)) { ////////////////////////////////////////////////////////////////////// // Step 2: Construct a hashtable containing a list of all the property-names in the ProfileBase type PropertyInfo[] baseProps = typeof(ProfileBase).GetProperties(); NameValueCollection baseProperties = new NameValueCollection(baseProps.Length); foreach (PropertyInfo baseProp in baseProps) { baseProperties.Add(baseProp.Name, String.Empty); } ////////////////////////////////////////////////////////////////////// // Step 3: For each property in the derived class, add it to the s_Properties class. PropertyInfo[] props = baseType.GetProperties(); foreach (PropertyInfo prop in props) { if (baseProperties[prop.Name] == null) //not in the base class { ProfileProvider prov = hasLowTrust ? ProfileManager.Provider : null; bool readOnly = false; SettingsSerializeAs serializeAs = SettingsSerializeAs.ProviderSpecific; string defaultValue = String.Empty; bool allowAnonymous = false; string customData = null; ////////////////////////////////////////////////////////////////////// // Step 4: For the property, get the attributes Attribute[] attribs = Attribute.GetCustomAttributes(prop, true); foreach (Attribute attrib in attribs) { if (attrib is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)attrib).SerializeAs; } else if (attrib is SettingsAllowAnonymousAttribute) { allowAnonymous = ((SettingsAllowAnonymousAttribute)attrib).Allow; if (!fAnonEnabled && allowAnonymous) { throw new ConfigurationErrorsException(SR.GetString(SR.Annoymous_id_module_not_enabled, prop.Name), config.ElementInformation.Properties["inherits"].Source, config.ElementInformation.Properties["inherits"].LineNumber); } } else if (attrib is System.ComponentModel.ReadOnlyAttribute) { readOnly = ((System.ComponentModel.ReadOnlyAttribute)attrib).IsReadOnly; } else if (attrib is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)attrib).Value; } else if (attrib is CustomProviderDataAttribute) { customData = ((CustomProviderDataAttribute)attrib).CustomProviderData; } else if (hasLowTrust && attrib is ProfileProviderAttribute) { prov = ProfileManager.Providers[((ProfileProviderAttribute)attrib).ProviderName]; if (prov == null) { throw new ConfigurationErrorsException(SR.GetString(SR.Profile_provider_not_found, ((ProfileProviderAttribute)attrib).ProviderName), config.ElementInformation.Properties["inherits"].Source, config.ElementInformation.Properties["inherits"].LineNumber); } } } ////////////////////////////////////////////////////////////////////// // Step 5: Add the property to the s_Properties SettingsAttributeDictionary settings = new SettingsAttributeDictionary(); settings.Add("AllowAnonymous", allowAnonymous); if (!string.IsNullOrEmpty(customData)) { settings.Add("CustomProviderData", customData); } SettingsProperty sp = new SettingsProperty(prop.Name, prop.PropertyType, prov, readOnly, defaultValue, serializeAs, settings, false, true); s_Properties.Add(sp); } } } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // Step 6: Add all properties from config if (config.PropertySettings != null) { AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, config.PropertySettings, null); foreach (ProfileGroupSettings pgs in config.PropertySettings.GroupSettings) { AddPropertySettingsFromConfig(baseType, fAnonEnabled, hasLowTrust, pgs.PropertySettings, pgs.Name); } } } catch (Exception e) { if (s_InitializeException == null) { s_InitializeException = e; } } // If there are no properties, create an empty collection. if (s_Properties == null) { s_Properties = new SettingsPropertyCollection(); } // Make the properties collection read-only s_Properties.SetReadOnly(); s_Initialized = true; } // Throw an exception if there was an exception during initialization if (s_InitializeException != null) { throw s_InitializeException; } }
private static void InitializeStatic() { if (!ProfileManager.Enabled || s_Initialized) { if (s_InitializeException != null) { throw s_InitializeException; } } else { lock (s_InitializeLock) { if (s_Initialized) { if (s_InitializeException != null) { throw s_InitializeException; } return; } try { ProfileSection profileAppConfig = MTConfigUtil.GetProfileAppConfig(); bool fAnonEnabled = HostingEnvironment.IsHosted ? AnonymousIdentificationModule.Enabled : true; Type inheritsFromType = InheritsFromType; bool hasLowTrust = HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low); s_Properties = new SettingsPropertyCollection(); AddPropertySettingsFromConfig(inheritsFromType, fAnonEnabled, hasLowTrust, ProfileManager.DynamicProfileProperties, null); if (inheritsFromType != typeof(ProfileBase)) { PropertyInfo[] properties = typeof(ProfileBase).GetProperties(); NameValueCollection values = new NameValueCollection(properties.Length); foreach (PropertyInfo info in properties) { values.Add(info.Name, string.Empty); } foreach (PropertyInfo info2 in inheritsFromType.GetProperties()) { if (values[info2.Name] == null) { ProfileProvider provider = hasLowTrust ? ProfileManager.Provider : null; bool isReadOnly = false; SettingsSerializeAs providerSpecific = SettingsSerializeAs.ProviderSpecific; string defaultValue = string.Empty; bool allow = false; string customProviderData = null; foreach (Attribute attribute in Attribute.GetCustomAttributes(info2, true)) { if (attribute is SettingsSerializeAsAttribute) { providerSpecific = ((SettingsSerializeAsAttribute)attribute).SerializeAs; } else if (attribute is SettingsAllowAnonymousAttribute) { allow = ((SettingsAllowAnonymousAttribute)attribute).Allow; if (!fAnonEnabled && allow) { throw new ConfigurationErrorsException(System.Web.SR.GetString("Annoymous_id_module_not_enabled", new object[] { info2.Name }), profileAppConfig.ElementInformation.Properties["inherits"].Source, profileAppConfig.ElementInformation.Properties["inherits"].LineNumber); } } else if (attribute is ReadOnlyAttribute) { isReadOnly = ((ReadOnlyAttribute)attribute).IsReadOnly; } else if (attribute is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)attribute).Value; } else if (attribute is CustomProviderDataAttribute) { customProviderData = ((CustomProviderDataAttribute)attribute).CustomProviderData; } else if (hasLowTrust && (attribute is ProfileProviderAttribute)) { provider = ProfileManager.Providers[((ProfileProviderAttribute)attribute).ProviderName]; if (provider == null) { throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_provider_not_found", new object[] { ((ProfileProviderAttribute)attribute).ProviderName }), profileAppConfig.ElementInformation.Properties["inherits"].Source, profileAppConfig.ElementInformation.Properties["inherits"].LineNumber); } } } SettingsAttributeDictionary attributes = new SettingsAttributeDictionary(); attributes.Add("AllowAnonymous", allow); if (!string.IsNullOrEmpty(customProviderData)) { attributes.Add("CustomProviderData", customProviderData); } SettingsProperty property = new SettingsProperty(info2.Name, info2.PropertyType, provider, isReadOnly, defaultValue, providerSpecific, attributes, false, true); s_Properties.Add(property); } } } if (profileAppConfig.PropertySettings != null) { AddPropertySettingsFromConfig(inheritsFromType, fAnonEnabled, hasLowTrust, profileAppConfig.PropertySettings, null); foreach (ProfileGroupSettings settings in profileAppConfig.PropertySettings.GroupSettings) { AddPropertySettingsFromConfig(inheritsFromType, fAnonEnabled, hasLowTrust, settings.PropertySettings, settings.Name); } } } catch (Exception exception) { if (s_InitializeException == null) { s_InitializeException = exception; } } if (s_Properties == null) { s_Properties = new SettingsPropertyCollection(); } s_Properties.SetReadOnly(); s_Initialized = true; } if (s_InitializeException != null) { throw s_InitializeException; } } }
public void SetProvider(string ProviderName) { OnDebug(this, name + ".SetProvider()"); try { if (String.IsNullOrWhiteSpace(ProviderName)) { InternalProvider = ProfileManager.Provider; } else { InternalProvider = ProfileManager.Providers[ProviderName]; } OnDebug(this, name + ": InternalProvider = " + InternalProvider.Name); } catch (Exception ex) { OnError(this, ex); InternalProvider = ProfileManager.Provider; } }
public ProfileService(ProfileProvider profileProvider) { this.profileProvider = profileProvider; }