Asp. NET State management X (Profile profiles)

Source: Internet
Author: User

In many applications, you need to store and use information that is unique to the user. When a user accesses a site, you can use the stored information to display a personalized version of the WEB application to the user. Personalization applications require a large number of elements: you must store information with a unique user identifier, identify the user when the user accesses it again, and then obtain user information as needed. To simplify your application, you can use the ASP.net profile feature, which can perform all of the above tasks for you.

The ASP.net profile feature associates information with a single user and stores the information in a persistent format. Configuration files allow you to manage user information without creating and maintaining your own database. In addition, the ASP.net profile feature enables you to use related user information by using a strongly typed API that can be accessed from anywhere in your application.

You can use a configuration file to store objects of any type. The profile feature provides a common storage feature that enables you to define and maintain almost any type of data while still using it in a type-safe manner.

How the asp.net configuration file works

To use a configuration file, you should first enable the configuration file by modifying the configuration file for the ASP.net Web application. To specify a profile provider in the configuration, the provider is the base class for performing low-level tasks such as storing and retrieving profile data. You can use the profile providers that are included in the. NET Framework to store profile data in SQL Server, or you can create and use your own profile provider as described in the implementation Profile provider topic. You can specify a SqlProfileProvider instance to connect to the selected database, or you can use the default SqlProfileProvider instance that stores the profile data on the local WEB server.

Configuration file functionality can be configured by defining a list of properties whose values you want to maintain. For example, you might want to store a user's zip code so that your application can provide locale-specific information, such as weather forecasts. In the configuration file, you should define a profile property named PostalCode. The profile section of the configuration file is similar to the following:

<profile>
<properties>
<add name= "PostalCode"/>
</properties>
</ Profile>

When the application runs, ASP.net creates a ProfileCommon class, which is a dynamically generated class that inherits from the ProfileBase class. The dynamic ProfileCommon class includes properties that are created based on the profile attribute defined in the application configuration. The instance of this dynamic ProfileCommon class is then set to the value of the profile property of the current HttpContext, and can be used in the application's page.

In your application, you can collect the values that you want to store and assign them to the profile properties that you have defined. For example, the home page of an application might contain a text box that prompts the user for a postal code. When a user enters a postal code, you can set the profile property to store the value of the current user, as shown in the following example:

Profile.PostalCode = Txtpostalcode.text;

When you set a value for Profile.PostalCode, the value is automatically stored for the current user. You do not need to write any code to determine the identity of the current user, nor do you need to explicitly store values in the database, which will perform these tasks for you.

If you want to use this value, you can get the value in the same way that you set the value. For example, the following code example demonstrates how to invoke a hypothetical function named Getweatherinfo to pass it to the postal code of the current user stored in the configuration file:

Weatherinfo = Getweatherinfo (Profile.PostalCode);

You do not need to explicitly determine the user identity or perform any database lookups. Simply getting the property value from the configuration file allows ASP.net to perform the necessary actions to identify the current user and find the value in the Persistence profile store.

Specific reference

Http://msdn.microsoft.com/en-us/library/ms379605.aspx

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.