Profile Custom Attributes

Source: Internet
Author: User
Tags guid identifier
Some basic user information can be stored in the membership table, but sometimes the user information we need to record is far more than the information 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. net2.0, we provide the personalized Settings feature-profile. Let's take a look at several features of the profile:

1) The profile stores the data of each user, including the data used for anonymity.

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. net2.0 verifies the identity of each login user and generates a guid for the anonymous request user. This is the code that uniquely identifies the user identity, so that 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 sqlserver2005 database and you will see two fields: propertynames and propertyvaluesstring. 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" user information

Step 1: Define the settings 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

Write profile to 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 and you will find that your custom information is added to it.

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 one group.

Step 1: Define the settings 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

Write profile to 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. blood;

}

Step 3: view the aspnet_profile table and you will find that your custom information is added to it.

3. Update the profile user settings file

Step 1: Set the Web. config file

Step 2: 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 and 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.

Query profile information

Step 1: Set the configuration file

Step 2: Get all users

Membershipusercollection users = membership. getallusers ();

The getallusers () method of the membership class is used here. This content has been mentioned in "user and role management" and will not be repeated here.

Step 3: Compile the users set and obtain the profile of each user

Foreach (membershipuser singleuser in Users)

{

Profilecommon USERPROFILE = profile. getprofile (singleuser. username );

Response. Write (USERPROFILE. Name );

Response. Write (USERPROFILE. Age );

Response. Write (USERPROFILE. School );

}

Read the profile of an anonymous user

An anonymous user also has a profile? The answer is yes. As mentioned above, ASP. net2.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 identify anonymous users, you must configure the following in the web. config file:

<System. Web>

<Anonymousidentification enabled = "true"/>

</System. Web>

After setting, you can use this. Request. anonymousid to retrieve the user's guid.

Scenarios where 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.

Using anonymous profile has two key points:

1) set the Enable attribute of anonymousidentification to true.

2) set the profile field attribute to allowanonymous = "true"

Use anonymous profile to store information:

Step 1: Set the Enable attribute of anonymousidentification to true

<Anonymousidentification enabled = "true" cookieless = "usecookies"> </anonymousidentification>

Step 2: Set "foreground color" and "background color" to "allowanonymous =" true "in the web. config file"

<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" of Anonymous Users"

Profile. 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.

Migration from an anonymous profile to a logged-on user (Migration)

Step 1:

Step 2:

Step 3:

Step 4: Add the following code to global. asax:

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.

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.