Since the logon control involves Membership, you have to mention the user's personalized Profile object. Personalization allows users to save specific personalized information to the database, so it is different from ASP. NET status management is that the information can be permanently stored. It is necessary to emphasize the difference between web application and website, directly use the Profile object (http://hi.baidu.com/windlhj/blog/item/8f4c4a13779de02fdc5401b7.html,web application and website convert http://blog.csdn.net/guwenzhong/archive/2009/11/10/4792814.aspx to each other ). Personalized settings are similar to those of Membership:
1. Configure the personalized provider
The default personalized provider is SqlProfileProvider, which uses ASP. NET. MDF (if you are not familiar with it or forget to see the settings of Membership) to store personalized information. After changing the database), your personal information is stored in the aspnet_profile table of the database. Like Membership, Profile personalized providers are configured in the root web. config. Because the database connection node has been configured before, only the personalized provider can be configured here:
<System. web>
<Profile defaultProvider = "MyFirstSqlProfileProvider">
<Providers>
<Clear/>
<Add name = "MyFirstSqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider"
ConnectionStringName = "newsystemConnectionString" applicationName = "ProfileDemo"/>
</Providers>
</Profile>
</System. web>
In this way, the personalized provider is configured, and its name is MyFirstSqlProfileProvider.
Since we want to store user personalized information to the aspnet_profile table, we must add attributes for it, these attributes must be added with the <add> label in <properties> of the <profile> node, with at least one attribute name:
<System. web>
<Profile defaultProvider = "MyFirstSqlProfileProvider">
<Providers>
<Clear/>
<Add name = "MyFirstSqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider"
ConnectionStringName = "newsystemConnectionString" applicationName = "ProfileDemo"/>
</Providers>
<Properties>
<Add name = "QQ" type = "string"/>
<Add name = "Age" type = "Int32"/>
<Add name = "Address" type = "string"/>
<Add name = "Tel" type = "string"/>
</Properties>
</Profile>
</System. web>
Name (attribute name, required) type (attribute type, default: string) serializeAs (format used for serialization)
ReadOnly (read-only attribute) defaultValue (default attribute value) allowAnonymous (Boolean value, whether anonymous users are allowed to read and set this attribute)
Provider (the personalized Provider associated with this attribute) customProviderData (allows users to transmit custom data to the personalized Provider)
Use personalized attributes
By default, only authenticated users can read and write personalized information to the database. For example:
// Directly assign a value to the profile to Save User Personalization, which is similar to session
Protected void button#click (object sender, EventArgs e)
{
// The profile object cannot be directly used in the webapp as in the website, but the introduction of using System. Web. Profile still cannot be used directly, but classes such as ProfileBase, ProfileManager, ProfileMigrateEventArgs can be used.
// Store personalized information
HttpContext. Current. Profile. SetPropertyValue ("QQ", TextBox9.Text );
HttpContext. Current. Profile. SetPropertyValue ("Age", int. Parse (DropDownList2.SelectedValue ));
HttpContext. Current. Profile. SetPropertyValue ("Tel", TextBox10.Text );
HttpContext. Current. Profile. SetPropertyValue ("Address", TextBox11.Text );
}
Protected void Button2_Click (object sender, EventArgs e)
{
// Obtain personalized information
Label1.Text = "current user:" + HttpContext. current. profile. userName + "QQ:" + HttpContext. current. profile. getPropertyValue ("QQ") + "Age:" + HttpContext. current. profile. getPropertyValue ("Age") + "Tel" + HttpContext. current. profile. getPropertyValue ("Tel") + "Address" + HttpContext. current. profile. getPropertyValue ("Address ");
}
You can also customize attribute groups for ease of management: Add <group> in <properties> web. config and then add group names and attributes:
<System. web>
<Profile defaultProvider = "MyFirstSqlProfileProvider">
<Providers>
<Clear/>
<Add name = "MyFirstSqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider"
ConnectionStringName = "newsystemConnectionString" applicationName = "ProfileDemo"/>
</Providers>
<Properties>
<Add name = "QQ" type = "string"/>
<Add name = "Age" type = "Int32"/>
<Add name = "Address" type = "string"/>
<Add name = "Tel" type = "string"/>
<Group name = "Habits">
<Add name = "likebooks"/>
<Add name = "likesports"/>
</Group>
</Properties>
</Profile>
</System. web>
The type of those attributes can also be self-written classes (the class can be in App_Code (App_Code is equivalent to the sub-namespace in the current namespace (pay attention to the attributes of the class in webapp) can be intelligently prompted only after being compiled )) it can also be in the compiled DLL (DLL is a form of Assembly (the other is exe) but in the Web. make sure to write the namespace of the entire class in config. class Name; otherwise, the type cannot be found ):
For example, <add name = "ZiDingDe" type = "Dingyibymyself. personlity"/> The namespace where the ZiDingDe type is written. The class name Dingyibymyself. personlity
Another example is the custom class <add name = "ZiDingDe2" type = "WebApplication1.App _ Code. personlity"/> In App_Code. Is it a sub-namespace?
http://www.cnblogs.com/xlb2000/archive/2010/08/25/1796405.html (Note the author's interpretation of serialization and Profile for custom classes at the end.) In the aspnet_proile table, PropertyNames contains the attribute name (including the custom attribute name) in PropertyValuesString, the value of a simple type property is the value of a custom type property in PropertyValuesBinary (at this time, the class object has been serialized in xml or binary format ). Because an object of the class (deserialized xml or binary) is obtained when the property value of the custom type is obtained ), therefore, to obtain the attribute values in a custom class, you must first create a reference to point to it, and then use the reference to obtain the attribute values in the Custom class: protected void button#click (object sender, EventArgs e) {person pp = (person) HttpContext. current. profile. getPropertyValue ("ZiDingDe"); Label1.Text = "current user:" + HttpContext. current. profile. userName + "QQ" + pp. QQ + "Age" + pp. age + "Tel" + pp. tel + "Address" + pp. address;} by default, anonymous users cannot save personalized information, but sometimes (for example, an anonymous user on the shopping network can also add a product to the shopping cart) You need to create temporary personalized information for anonymous users. ASP. NET can generate a random identifier for an anonymous user to uniquely identify the anonymous user and save the personalized information of the anonymous user to the database. However, anonymous personalization saves a lot of useless information in the database. Enable Anonymous PERSONALIZATION: add the <anonymousIdentification enable = "true"/> element under the <system. web> node of the root web. config. In <properties>, specify the attributes that can be used by anonymous users to save information (for example, <add name = "Book" allowAnonymous = "true"/> ). However, when an anonymous user saves these personalized attributes, it can be processed as if the user saved them. Profile: http://www.cnblogs.com/jason-xiao/archive/2009/03/18/1415343.html Imagine if this is the case, then there will be no practical effect, and some useless information will be added in vain? (For example, if an anonymous user adds some items to the shopping cart while they are anonymous, then the user logs on again, the previously added product will be copied to its user account.) So ASP. NET provides Profile. the MigrateAnonymous event (this event is triggered only when the anonymous identity is valid and the current user is verified) can be performed at Global. asax (global application file: This file contains the response ASP. the application-level and session-Level Event code triggered by the NET or HTTP module should be created only when you want to process application or session events, this event mainly handles three steps: 1. call Profile. getProfile method to obtain personalized information of anonymous users 2. copy the personalized information to the personalized information of anonymous users after logon. 3. remove the personalized information when you are anonymous, and clear the anonymous tag. Global. system. web. profile, MigrateAnonymous Event code: protected void Profile_OnMigrateAnonymous (object sender, ProfileMigrateEventArgs e) {// webapp still cannot use the ProfileCommon class. It seems that this can use Web Profile Builder. // Obtain the personalized information of anonymous users. ProfileCommon anonProfile = Profile. getProfile (e. anonymousID); // copy the personalized information of the anonymous user to the Profile of the user after logon. user attribute = anonProfile. when the user is anonymous, attributes // The personalized information and the anonymous identifier ProfileManager are deleted. deleteProfile (e. anonymousID); AnonymousIdentificationModule. clearAnonymousIdentifier (); // deletes the anonymous user Membership generated when the user is anonymous. deleteUser (e. anonymousID, true);} automatically saves personalized information by default ASP.. NET automatically saves personalized information whenever the lifecycle of a page ends, regardless of whether the personalized information changes or not, which wastes resources. Therefore, we need to make it possible to save personalized information at the end of the page lifecycle only when personalized information is changed: 1. set automationSaveEnabled = "false" in the <profile> node to prevent it from automatically saving personalized information (that is, we manually call the method to save it. manually call the Save Method: After the personalized information is changed, the call HttpContext is displayed. current. profile. save () for manual control. For example: protected void button#click (object sender, EventArgs e) {HttpContext. current. profile. setPropertyValue ("QQ", TextBox9.Text); HttpContext. current. profile. setPropertyValue ("Age", int. parse (DropDownList2.SelectedValue); HttpContext. current. profile. setPropertyValue ("Tel", TextBox10.Text); HttpContext. current. profile. setPropertyValue ("Address", TextBox11.Text); HttpContext. current. profile. save (); // just add this sentence}