Profile mechanism of Asp.net 2.0

Source: Internet
Author: User
Tags guid identifier

The profile mechanism of Asp.net 2.0 can store some basic user information in the membership table, but sometimes we need to record the user information much more than what is provided in the membership table, such as QQ, MSN, home address, and contact number. So how to record the user information to the database? In Asp.net 2.0, profile is provided for us. Let's take a look at several features of the Profile: 1) the profile stores user data based on each user, including the data used for anonymous name. 2) The profile can be defined in Web. config and takes effect immediately without manual expansion of database fields. 3) profile can store any data type, including simple data type and custom complex data type. How does profile implement the above functions? Asp.net 2.0 To authenticate the identity of each login user and generate a guid for the anonymous request user. This is the code that uniquely identifies the user's identity. In this way, the user in each request is invisible, and their identity IDs do not interfere with each other. So how does Asp.net expand other user information without expanding fields?Open the aspnet_profile table in the SQL Server 2005 database and you will see the propertynames and propertyvaluesstring fields. The propertyvaluesstring field stores all the information about your new user data. It is stored as a text stream, and the propertynames field describes how to parse the content of the propertyvaluesstring field, it also exists as a text stream. In this way, you can customize any field and write the information in the table. The following describes how to read and write profile files: 1. Expand the "Real Name", "Age", and "school" Custom User information. Step 1: define the setting file <system. web> <profile>   <Properties>    <Add name = "name" type = "system. String"> </Add>    <Add name = "Age" type = "system. int32"> </Add>    <Add name = "school" type = "system. String"> </Add>    </Properties></Profile> </system. web> Step 2: Use profile in vs2005 to write the profile to the database if (user. identity. isauthenticated) {profile. name = txtname. text; profile. age = convert. toint32 (txtage. text); profile. school = txtschool. text;} Read the profile from the database to the page if (user. identity. isauthenticated) {txtname. TEXT = profile. name; txtage. TEXT = profile. age. tostring (); txtschool. TEXT = profile. school;} Step 3: view the aspnet_profile table. You will find that your custom information is added to the table. 2. Add the birth date and blood type custom data to the existing custom data. The birth date and blood type can be set as a group. Step 1: Define the configuration file <system. web> <profile>   <Properties>    <Add name = "name" type = "system. String"> </Add>    <Add name = "Age" type = "system. int32"> </Add>    <Add name = "school" type = "system. String"> </Add>    <Group name = "other"><Add name = "Birthday" type = "system. datetime"> </Add> <Add name = "blood" type = "string"> </Add>   </Group>    </Properties></Profile> </system. web> Step 2: Use profile in vs2005 to write the profile to the database if (user. identity. isauthenticated) {profile. name = txtname. text; profile. age = convert. toint32 (txtage. text); profile. school = txtschool. text; profile. other. birthday = convert. todatetime (txtbirthday. text); profile. other. blood = txtblood. text;} Read the profile from the database to the page if (user. identity. isauthenticated) {txtname. TEXT = profile. name; txtage. TEXT = profile. age. Tostring (); txtschool. TEXT = profile. school; txtbirthday. TEXT = profile. other. birthday. tostring (); txtblood. TEXT = profile. other. step 3: view the aspnet_profile table. You will find that your custom information is added to the table. 3. Step 1: Set the web. step 2 of the config file: add the update code if (user. identity. isauthenticated) {profile. name = txtname. text; profile. age = convert. toint32 (txtage. text); profile. school = txtschool. text; profile. other. birthday = convert. todatetime (txtbirthday. text); profile. other. blood = txtblood. text; profile. save ();} Step 3: view the aspnet_profile table. You will find that your custom information has been modified. We use profile. age and other ways to read the user's age and other information, but sometimes we want to query and display all user information, but Asp.net does not provide the function to query all user information, we can only query the profile information of existing users one by one. Step 1: Set the configuration file Step 2: Get all user membershipusercollection Users = membership. getallusers ();The getallusers () method of the membership class is used here. Step 3: traverse the users set and retrieve the profileforeach (membershipuser singleuser in users) of each user) {   Profilecommon USERPROFILE = profile. getprofile (singleuser. username );    Response. Write (USERPROFILE. Name );    Response. Write (USERPROFILE. Age );    Response. Write (USERPROFILE. School );} Is there a profile for an anonymous user to read the profile of an anonymous user? The answer is yes. As mentioned above, Asp.net 2.0 is added with an anonymous tracking mechanism, which can generate a unique guid identifier appended to the request of an unverified webpage. The default "anonymous identity recognition" is disabled. Therefore, if you want your website to recognize anonymous users. in the config file: <system. web> <anonymousidentification enabled = "true"/> </system. web> after setting, you can use this. request. anonymousid retrieves the user's guid. Scenarios where an anonymous user profile is used: 1) when we used to make a shopping website, we put the shopping cart in the session, but the session has a certain expiration Policy, generally 20 minutes. If we check out the shopping cart for half an hour, it is best to store the data in the shopping cart through profile. 2) When someone visits a shopping website and does not like to log on to the website to purchase another product, they may find a good thing. If they log on to the website to purchase the product, they will be a little annoyed, especially when the network speed is relatively slow. In this case, you can use an anonymous identity to record the items purchased by the user, and then allow the user to enter the account and password during the checkout. There are two key points for using anonymous profile: 1) Set the Enable attribute of anonymousidentification to true.2) Set the profile field attribute to allowanonymous = "true"Store information using anonymous profile: Step 1: Set the Enable attribute of anonymousidentification to true <anonymousidentification enabled = "true" cookieless = "usecookies"> </anonymousidentification> Step 2: on the web. in the config file, set "foreground color" and "background color" to allowanonymous = "true" <profile>   <Properties>    <Add name = "name" type = "system. String"> </Add>    <Add name = "Age" type = "system. int32"> </Add>    <Add name = "school" type = "system. String"> </Add>    <Group name = "color"><Add name = "forecolor" type = "system. drawing. color "serializeas =" binary "allowanonymous =" true "> </Add> <Add name =" backcolor "type =" system. drawing. color "serializeas =" binary "allowanonymous =" true "> </Add>   </Group>    </Properties>    <Profile>Step 3: Set the "foreground color" and "background color" Profile for anonymous users. color. backcolor = color. fromname (listback. text); profile. color. forecolor = color. fromname (listfore. text); label1.forecolor = profile. color. forecolor; label1.backcolor = profile. color. backcolor; Step 4: view the content in the aspnet_profile table and find that the color is saved in the table. Step 1: Add the following code to the Global. asax of the website:   Void profile_migrateanonymous (Object sender, profilemigrateeventargs ARGs){   // Obtain the ID of an anonymous user    Profilecommon anonyprofile = profile. getprofile (ARGs. anonymousid );    If (anonyprofile. color. backcolor! = NULL) & (anonyprofile. color. forecolor! = NULL ))    {Profile. color. forecolor = anonyprofile. color. forecolor; profile. color. backcolor = anonyprofile. color. backcolor; profile. Save ();   }    // Delete the profile of an anonymous user    Profilemanager. deleteprofile (ARGs. anonymousid );    // Clear cookie or identity information of Anonymous Users    Anonymousidentificationmodule. clearanonymousidentifier (); } When a user logs on, the profile_migrateanonymous event is triggered to migrate anonymous users to the user's profile.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.