Key
1. Configure the <profile> element under the <system. web> element. To support anonymity, configure the <anonymousidentif> element under the <system. web> element. Example:
<Profile enabled = "true" defaultProvider = "SqlProfileProvider" inherits = "CustomProfile">
<Providers>
<Add name = "SqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider, System. Web, Version = 2.0.3600.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"
ConnectionStringName = "SqlConnectionString"
ApplicationName = "/"/>
</Providers>
<Properties>
<Add name = "Name"/>
<Add name = "Color" type = "System. Drawing. Color"/>
<Group name = "Group">
<Add name = "Collection" type = "System. Collections. ArrayList"/>
<Add name = "Price" type = "int" defaultValue = "100"/>
</Group>
</Properties>
</Profile>
<AnonymousIdentification
Enabled = "true"
CookieName = ". VS2005_ANONYMOUS"
CookieTimeout = "1440"
CookiePath = "/"
CookieRequireSSL = "false"
CookieSlidingExpiration = "true"
CookieProtection = "All"
Cookieless = "UseCookies"/>
<Profile> the inherits attribute of an element specifies a custom class that inherits from ProfileBase.
The Profile is automatically saved, but some complex types may not be automatically saved. In this case, set the <profile> element's automaticSaveEnabled to false. To Save the Profile, call the Save method on the Profile. To dynamically cancel the automatic saving function of Profile, you need to add a Profile_ProfileAutoSaving event in global. asax. The example is as follows:
Void Profile_ProfileAutoSaving (Object sender, ProfileAutoSaveEventArgs e)
{
If (e. Context. Items ["CancelProfileAutoSave"]! = Null) & (bool) e. Context. Items ["CancelProfileAutoSave"] = true ))
E. ContinueWithProfileAutoSave = false;
}
Write the following code on the page on which you want to cancel the Automatic save feature of Profile:
Protected void Page_Load (object sender, EventArgs e)
{
Context. Items ["CancelProfileAutoSave"] = true;
}
2. Execute related tasks through ProfileManager, such as searching statistics on all configuration files, configuration files of authenticated users, and configuration files of anonymous users, determine the number of configuration files that have not been modified within the specified period, and delete a single configuration file and multiple configuration files based on the last modification date of the configuration file.
3. migrate anonymous configuration files to authenticated configuration files
Add a Profile_MigrateAnonymous event to global. asax for processing.
Void Profile_MigrateAnonymous (Object sender, ProfileMigrateEventArgs pe)
{
// Obtain anonymous Configuration
ProfileCommon anonProfile = Profile. GetProfile (pe. AnonymousID );
// Value from anonymous configuration and assign it to authenticated Configuration
If (anonProfile. Color! = System. Drawing. Color. Empty)
{
Profile. Color = anonProfile. Color;
}
// Delete anonymous configurations from the database
ProfileManager. DeleteProfile (pe. AnonymousID );
// Clear the anonymous Cookie or identifier associated with a session
AnonymousIdentificationModule. ClearAnonymousIdentifier ();
}
Example
App_Code/CustomProfile. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Web. Profile;
/** // <Summary>
/// Summary of CustomProfile
/// </Summary>
Public class CustomProfile: ProfileBase
{
Private string _ customName;
Public string CustomName
{
Get {return (string) base ["CustomName"];}
Set {base ["CustomName"] = value ;}
}
Private bool _ customSex;
Public bool CustomSex
{
Get {return (bool) base ["CustomSex"];}
Set {base ["CustomSex"] = value ;}
}
}
Web. config
<Profile enabled = "true" defaultProvider = "SqlProfileProvider" inherits = "CustomProfile">
<Providers>
<Add name = "SqlProfileProvider" type = "System. Web. Profile. SqlProfileProvider, System. Web, Version = 2.0.3600.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"
ConnectionStringName = "SqlConnectionString"
ApplicationName = "/"/>
</Providers>
<Properties>
<Add name = "Name"/>
<Add name = "Color" type = "System. Drawing. Color"/>
<Group name = "Group">
<Add name = "Collection" type = "System. Collections. ArrayList"/>
<Add name = "Price" type = "int" defaultValue = "100"/>
</Group>
</Properties>
</Profile>
Profile/Test. aspx
<% @ Page Language = "C #" MasterPageFile = "~ /Site. master "AutoEventWireup =" true "CodeFile =" Test. aspx. cs"
Inherits = "Profile_Test" Title = "Storage user configuration test" %>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" runat = "Server">
<Asp: Label ID = "lbl" runat = "Server"/>
</Asp: Content>
Profile/Test. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class Profile_Test: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Understand at a glance
Profile. Name = User. Identity. Name;
Profile. Color = System. Drawing. Color. AliceBlue;
Profile. Group. Collection. Clear ();
Profile. Group. Collection. Add ("Popsicle ");
Profile. Group. Collection. Add ("melon seeds ");
Profile. Group. Price = 999999;
Profile. CustomName = User. Identity. Name;
Profile. CustomSex = true; www.2cto.com
Lbl. Text = "Name:" + Profile. Name + "<br/> ";
Lbl. Text + = "Color:" + Profile. Color. ToString () + "<br/> ";
Foreach (string s in Profile. Group. Collection)
{
Lbl. Text + = "products:" + s + "<br/> ";
}
Lbl. Text + = "Price:" + Profile. Group. Price + "<br/> ";
Lbl. Text + = "Custom class name:" + Profile. CustomName + "<br/> ";
Lbl. Text + = "Custom class name:" + Profile. CustomSex;
}
}
Running result after logon using the "abc" User
Name: abc
Color: Color [AliceBlue]
Products: popsicles
Products: melon seeds
Price: 999999
Custom Class Name: abc
Custom Class Name: True
Note: You need to use aspnet_regsql to configure the database.
OK
By niemeiquan