Profile customization for ASP. NET users -- configuration 1

Source: Internet
Author: User

ASP. NET 2.0 provides a membership authentication mechanism and a user-defined profile mechanism. Profile can be combined with membership to store user-defined data. The profile can store various types of data, including the most basic int and double types, or complex user-defined types, such as shopping cart and user information.

Profile is similar to session, which is used to store user-defined data, but there are also essential differences between them:

1. the session can only retain user data for a short time (20 minutes by default), but the profile can be permanently saved. Because the two storage media are different, the session is stored in the memory by default, the profile is stored in the database. Although the session can be saved to the status server or database through configuration, there is a big difference.

2. profile is strongly typed, and session is just a set of items. In addition, vs provides smart sensing support for profile for ease of use.

 

Next, let's take a look at how to use the profile. The first step is the same as membership, but it is also the same as membership, the second step is configuration. If not, see the previous article to add database support. Set the followingCodeCopy to the system. Web node of Web. config:

< Profile Enabled = "True" Defaultprovider = "Sqlprovider" >
< Providers >
< Add Name = "Sqlprovider" Type = "System. Web. profile. sqlprofileprovider" Connectionstringname = "Connectionstring" Applicationname = "Testmembership" />
</ Providers >
< Properties >
< Add Name = "Test" Type = "String" Allowanonymous = "True" />
</ Properties >
</ Profile >

 

Most of the attributes of this configuration have the same meaning as those of membership. We just want to describe the unique attributes. In this configuration, there are providers and properties nodes. The Providers node has been learned before in configuring membership. Now we will introduce the properties nodes:

Under this node, the Add label is used to add an "attribute". The name attribute is the name of the "attribute", and the type is the type of the "attribute, this type can be int, double, or other simple data types, or user-defined data types such as shopping cart, but note that if it is a "class" type, the class must be serializable, that is, the [serializable] feature is added to the class. The allowanonymous attribute specifies whether users can set this attribute anonymously.

 

After the configuration is complete, we can use profile on the page, create a page, and add a button on the page to double-click the button and write the following code in the Click Event of the button:

Profile. Test =   " 123123 " ;

This profile. test is the "attribute" We added under the properties node. The type is string.

Browse this page and click "button" to see the effect! Error! The error message is as follows:

The configuration file property "test" allows anonymous users to store data. This requires that the anonymousidentiffeature be enabled.

 

This is because ASP.. Net by default, anonymous users are not allowed. It is very simple. Simply set it to allow anonymous users. system. add a configuration under the Web node:

< Anonymousidentification Enabled = "True" />

OK. Now, go to the page and click "button". How can this problem be solved? No response? That's right!

Create a page again, and add response. Write (profile. Test) in the page_load event of the page );

After that, we first browsed the first page, clicked the button, and executed the profile. test = "123123"; this code section closes the browser and opens the second page. We can see the string "123123" that we saved in the first page, the second page is displayed.

 

The principle is simple: After the anonymousidentification function is enabled, ASP. NET assigns an anonymousid to each user and stores it in the cookie.

This is the content stored in the cookie. aspxanonymous is the default cookie name. You can set your own cookie name in the cookiename attribute in the anonymousidentifsection of Web. config.

This is the anonymousid obtained in the debugging status.

This is the content stored in the aspnet_users table. Pay attention to the username and isanonymous fields.

This is the content in aspnet_profile of the database. We can see that the content in the test attribute is: 123123, and the meaning of test: S: 0: 6 in the propertynames field should be guessed! Test is the property name, and s represents the string type. 0: 6 represents the content in the propertyvaluesstring field from 0th bits to 6th bits.

In this way, when the user uses this computer to access the website again, the website can identify the user based on the anonymousid in the cookie, and then from the aspnet_profile table through anonymousid, you can retrieve the data you saved last time.

 

OK. The above is the profile in the anonymous state. Of course, if we do not need to be anonymous and require users to log on before they can save the data, it will not be so troublesome. We can directly set allowanonymous= "False"You do not need to addAnonymousidentificationConfiguration. In this way, this attribute can only be accessed when the user logs on. If the user does not log on to this attribute, an error is returned.

 

This articleArticleThis article briefly introduces the application of profile in actual projects. You can give it a try. First, you will have a perceptual knowledge. Next, you will have a small project, to demonstrate the profile application.

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.