// void PutCustomProfileInContext (HttpContext context, string assemblyName) // { // Type type = Type.GetType (String.Format ("ProfileCommon, {0}", // Path.GetFileNameWithoutExtension (assemblyName))); // ProfileBase pb = Activator.CreateInstance (type) as ProfileBase; // if (pb != null) // context.Profile = pb; // } public static bool HaveCustomProfile (ProfileSection ps) { if (ps == null || !ps.Enabled) return false; RootProfilePropertySettingsCollection props = ps.PropertySettings; ProfileGroupSettingsCollection groups = props != null ? props.GroupSettings : null; if (!String.IsNullOrEmpty (ps.Inherits) || (props != null && props.Count > 0) || (groups != null && groups.Count > 0)) return true; return false; }
void BuildProfileClass (ProfileSection ps, string className, ProfilePropertySettingsCollection psc, CodeNamespace ns, string baseClass, bool baseIsGlobal, SortedList <string, string> groupProperties) { CodeTypeDeclaration profileClass = new CodeTypeDeclaration (className); CodeTypeReference cref = new CodeTypeReference (baseClass); if (baseIsGlobal) cref.Options |= CodeTypeReferenceOptions.GlobalReference; profileClass.BaseTypes.Add (cref); profileClass.TypeAttributes = TypeAttributes.Public; ns.Types.Add (profileClass); foreach (ProfilePropertySettings pset in psc) AddProfileClassProperty (ps, profileClass, pset); if (groupProperties != null && groupProperties.Count > 0) foreach (KeyValuePair <string, string> group in groupProperties) AddProfileClassGroupProperty (group.Key, group.Value, profileClass); AddProfileClassGetProfileMethod (profileClass); }
// FIXME: there should be some validation of syntactic correctness of the member/class name // for the groups/properties. For now it's left to the compiler to report errors. // // CodeGenerator.IsValidLanguageIndependentIdentifier (id) - use that // bool ProcessCustomProfile (ProfileSection ps, AppCodeAssembly defasm) { CodeCompileUnit unit = new CodeCompileUnit (); CodeNamespace ns = new CodeNamespace (null); unit.Namespaces.Add (ns); defasm.AddUnit (unit); ns.Imports.Add (new CodeNamespaceImport ("System")); ns.Imports.Add (new CodeNamespaceImport ("System.Configuration")); ns.Imports.Add (new CodeNamespaceImport ("System.Web")); ns.Imports.Add (new CodeNamespaceImport ("System.Web.Profile")); RootProfilePropertySettingsCollection props = ps.PropertySettings; if (props == null) return true; SortedList<string, string> groupProperties = new SortedList<string, string> (); string groupName; foreach (ProfileGroupSettings pgs in props.GroupSettings) { groupName = MakeGroupName (pgs.Name); groupProperties.Add (groupName, pgs.Name); BuildProfileClass (ps, groupName, pgs.PropertySettings, ns, "System.Web.Profile.ProfileGroupBase", true, null); } string baseType = ps.Inherits; if (String.IsNullOrEmpty (baseType)) baseType = "System.Web.Profile.ProfileBase"; else { string[] parts = baseType.Split (new char[] {','}); if (parts.Length > 1) baseType = parts [0].Trim (); } bool baseIsGlobal; if (baseType.IndexOf ('.') != -1) baseIsGlobal = true; else baseIsGlobal = false; BuildProfileClass (ps, "ProfileCommon", props, ns, baseType, baseIsGlobal, groupProperties); return true; }
void GetProfileSettingsSerializeAsAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection, SerializationMode mode) { string parameter = String.Concat ("SettingsSerializeAs.", mode.ToString ()); collection.Add ( new CodeAttributeDeclaration ( "SettingsSerializeAs", new CodeAttributeArgument ( new CodeSnippetExpression (parameter) ) ) ); }
void AddProfileClassProperty (ProfileSection ps, CodeTypeDeclaration profileClass, ProfilePropertySettings pset) { string name = pset.Name; if (String.IsNullOrEmpty (name)) throw new HttpException ("Profile property 'Name' attribute cannot be null."); CodeMemberProperty property = new CodeMemberProperty (); string typeName = pset.Type; if (typeName == "string") typeName = "System.String"; property.Name = name; property.Type = GetProfilePropertyType (typeName); property.Attributes = MemberAttributes.Public; CodeAttributeDeclarationCollection collection = new CodeAttributeDeclarationCollection(); GetProfileProviderAttribute (ps, collection, pset.Provider); GetProfileSettingsSerializeAsAttribute (ps, collection, pset.SerializeAs); property.CustomAttributes = collection; CodeMethodReturnStatement ret = new CodeMethodReturnStatement (); CodeCastExpression cast = new CodeCastExpression (); ret.Expression = cast; CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression ( new CodeThisReferenceExpression (), "GetPropertyValue"); CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression ( mref, new CodeExpression[] { new CodePrimitiveExpression (name) } ); cast.TargetType = new CodeTypeReference (typeName); cast.Expression = minvoke; property.GetStatements.Add (ret); if (!pset.ReadOnly) { mref = new CodeMethodReferenceExpression ( new CodeThisReferenceExpression (), "SetPropertyValue"); minvoke = new CodeMethodInvokeExpression ( mref, new CodeExpression[] { new CodePrimitiveExpression (name), new CodeSnippetExpression ("value") } ); property.SetStatements.Add (minvoke); } profileClass.Members.Add (property); }
void GetProfileProviderAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection, string providerName) { if (String.IsNullOrEmpty (providerName)) providerTypeName = FindProviderTypeName (ps, ps.DefaultProvider); else providerTypeName = FindProviderTypeName (ps, providerName); if (providerTypeName == null) throw new HttpException (String.Format ("Profile provider type not defined: {0}", providerName)); collection.Add ( new CodeAttributeDeclaration ( "ProfileProvider", new CodeAttributeArgument ( new CodePrimitiveExpression (providerTypeName) ) ) ); }
string FindProviderTypeName (ProfileSection ps, string providerName) { if (ps.Providers == null || ps.Providers.Count == 0) return null; ProviderSettings pset = ps.Providers [providerName]; if (pset == null) return null; return pset.Type; }
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; } }
public static void Main() { // Process the System.Web.Configuration.ProfileSectionobject. try { // Get the Web application configuration. System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/aspnet"); // Get the section. System.Web.Configuration.ProfileSection profileSection = (System.Web.Configuration.ProfileSection) configuration.GetSection("system.web/profile"); // <Snippet4> // Get the current AutomaticSaveEnabled property value. Console.WriteLine( "Current AutomaticSaveEnabled value: '{0}'", profileSection.AutomaticSaveEnabled); // Set the AutomaticSaveEnabled property to false. profileSection.AutomaticSaveEnabled = false; // </Snippet4> // <Snippet5> // Get the current DefaultProvider property value. Console.WriteLine( "Current DefaultProvider value: '{0}'", profileSection.DefaultProvider); // Set the DefaultProvider property to "AspNetSqlProvider". profileSection.DefaultProvider = "AspNetSqlProvider"; // </Snippet5> // <Snippet6> // Get the current Inherits property value. Console.WriteLine( "Current Inherits value: '{0}'", profileSection.Inherits); // Set the Inherits property to // "CustomProfiles.MyCustomProfile, CustomProfiles.dll". profileSection.Inherits = "CustomProfiles.MyCustomProfile, CustomProfiles.dll"; // </Snippet6> // <Snippet7> // <Snippet10> // Display all current root ProfilePropertySettings. Console.WriteLine("Current Root ProfilePropertySettings:"); int rootPPSCtr = 0; foreach (ProfilePropertySettings rootPPS in profileSection.PropertySettings) { Console.WriteLine(" {0}: ProfilePropertySetting '{1}'", ++rootPPSCtr, rootPPS.Name); } // </Snippet10> // <Snippet11> // <Snippet14> // Get and modify a root ProfilePropertySettings object. Console.WriteLine( "Display and modify 'LastReadDate' ProfilePropertySettings:"); ProfilePropertySettings profilePropertySettings = profileSection.PropertySettings["LastReadDate"]; // </Snippet14> // <Snippet15> // Get the current ReadOnly property value. Console.WriteLine( "Current ReadOnly value: '{0}'", profilePropertySettings.ReadOnly); // Set the ReadOnly property to true. profilePropertySettings.ReadOnly = true; // </Snippet15> // <Snippet16> // Get the current AllowAnonymous property value. Console.WriteLine( "Current AllowAnonymous value: '{0}'", profilePropertySettings.AllowAnonymous); // Set the AllowAnonymous property to true. profilePropertySettings.AllowAnonymous = true; // </Snippet16> // <Snippet17> // Get the current SerializeAs property value. Console.WriteLine( "Current SerializeAs value: '{0}'", profilePropertySettings.SerializeAs); // Set the SerializeAs property to SerializationMode.Binary. profilePropertySettings.SerializeAs = SerializationMode.Binary; // </Snippet17> // <Snippet18> // Get the current Type property value. Console.WriteLine( "Current Type value: '{0}'", profilePropertySettings.Type); // Set the Type property to "System.DateTime". profilePropertySettings.Type = "System.DateTime"; // </Snippet18> // <Snippet19> // Get the current DefaultValue property value. Console.WriteLine( "Current DefaultValue value: '{0}'", profilePropertySettings.DefaultValue); // Set the DefaultValue property to "March 16, 2004". profilePropertySettings.DefaultValue = "March 16, 2004"; // </Snippet19> // <Snippet20> // Get the current ProviderName property value. Console.WriteLine( "Current ProviderName value: '{0}'", profilePropertySettings.Provider); // Set the ProviderName property to "AspNetSqlRoleProvider". profilePropertySettings.Provider = "AspNetSqlRoleProvider"; // </Snippet20> // <Snippet21> // Get the current Name property value. Console.WriteLine( "Current Name value: '{0}'", profilePropertySettings.Name); // Set the Name property to "LastAccessDate". profilePropertySettings.Name = "LastAccessDate"; // </Snippet21> // </Snippet11> // <Snippet12> // Display all current ProfileGroupSettings. Console.WriteLine("Current ProfileGroupSettings:"); int PGSCtr = 0; foreach (ProfileGroupSettings propGroups in profileSection.PropertySettings.GroupSettings) { Console.WriteLine(" {0}: ProfileGroupSetting '{1}'", ++PGSCtr, propGroups.Name); int PPSCtr = 0; foreach (ProfilePropertySettings props in propGroups.PropertySettings) { Console.WriteLine(" {0}: ProfilePropertySetting '{1}'", ++PPSCtr, props.Name); } } // </Snippet12> // <Snippet13> // Add a new group. ProfileGroupSettings newPropGroup = new ProfileGroupSettings("Forum"); profileSection.PropertySettings.GroupSettings.Add(newPropGroup); // <Snippet22> // Add a new PropertySettings to the group. ProfilePropertySettings newProp = new ProfilePropertySettings("AvatarImage"); newProp.Type = "System.String, System.dll"; newPropGroup.PropertySettings.Add(newProp); // </Snippet22> // <Snippet1> // Remove a PropertySettings from the group. newPropGroup.PropertySettings.Remove("AvatarImage"); newPropGroup.PropertySettings.RemoveAt(0); // </Snippet1> // <Snippet2> // Clear all PropertySettings from the group. newPropGroup.PropertySettings.Clear(); // </Snippet2> // </Snippet13> // </Snippet7> // <Snippet8> // Display all current Providers. Console.WriteLine("Current Providers:"); int providerCtr = 0; foreach (ProviderSettings provider in profileSection.Providers) { Console.WriteLine(" {0}: Provider '{1}' of type '{2}'", ++providerCtr, provider.Name, provider.Type); } // Add a new provider. profileSection.Providers.Add(new ProviderSettings("AspNetSqlProvider", "...SqlProfileProvider")); // </Snippet8> // <Snippet9> // Get the current Enabled property value. Console.WriteLine( "Current Enabled value: '{0}'", profileSection.Enabled); // Set the Enabled property to false. profileSection.Enabled = false; // </Snippet9> // Update if not locked. if (!profileSection.SectionInformation.IsLocked) { configuration.Save(); Console.WriteLine("** Configuration updated."); } else { Console.WriteLine("** Could not update, section is locked."); } } catch (System.ArgumentException e) { // Unknown error. Console.WriteLine( "A invalid argument exception detected in UsingProfileSection Main. Check your"); Console.WriteLine("command line for errors."); } }
static ProfileManager () { config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile"); }