Use application services and localization in ASP. net ajax (3): User personalized component ProfileService

Source: Internet
Author: User
This article is from Chapter 5 Application Service and localization of ASP. net ajax program design Article II: client-side Microsoft AJAX Library.

ASP. NET 2.0 has provided initial support for user management after the identity authentication application service is created. However, the user information is not only the user name and password. For general websites, we need to provide custom attributes associated with user accounts. For example, in a BBS program, such custom attributes include user points, grades, signatures, and portraits. These custom attributes correspond to user accounts one by one. To this end, ASP. NET 2.0 has built-in personalized application services, which are also provided to developers in a unified manner. The configuration and use of ASP. NET 2.0 built-in user-defined personalized application services are very simple, and flexible enough scalability is also provided.

Reference: to learn more about ASP. NET 2.0 User Personalization Services, see this MSDN Article ASP. NET Profile Properties (http://msdn2.microsoft.com/en-us/library/at64shx3.aspx ).

The user personalized service of ASP. net ajax framework can be integrated with the user personalized Application Service of ASP. NET 2.0, and provide it with the client's JavaScript call proxy. Some user-defined proxy functions of the ASP. net ajax client are provided by the client's ProfileService object.

The full name of the ProfileService object is Sys. Services. ProfileService. Similar to the AuthenticationService object, ProfileService is a Singleton mode object that can be used without manual instance creation. As long as the page contains the ScriptManager control, we can directly access the ProfileService object on the client, and indirectly deal with the server-side user personalized service and use ASP.. NET 2.0 provides user personalized service-related functions, such as loading or saving user personalized attributes-ASP. net ajax framework will be responsible for the implementation details of the entire asynchronous communication, just like ASP. the net ajax asynchronous communication layer generates client proxies for Web Services.

The ProfileService object provides two methods: load () and save (), which are used to load and save the user's personalized attributes respectively. It also provides a field named properties, it is used to access user personalized attributes on the client in a way similar to "Strong Access Mode" on the server side. In addition, ProfileService exposes a series of common attributes. Next we will introduce them one by one:

5.3.1Load ()Method

The load () method is used to load the personalized attributes of the current user. After loading, we can access the personalized attributes of each user that has been loaded on the client through the properties attribute of the ProfileService object. The complete syntax for calling the load () method is as follows:

Sys.Services.ProfileService.load(
    propertyNames, 
    loadCompletedCallback, 
    failedCallback, 
    userContext
);

The meanings of parameters are shown in Table 5-4.

Table 5-4 parameters of the load () method of the ProfileService object

  1. PropertyNames: An array containing string objects, indicating a set of user personalized attributes to be loaded from the server. If this parameter is null, ASP. net ajax automatically loads all user-defined attributes that can be read by the client. This section describes how to selectively expose personalized attributes of users in ASP. NET 2.0 to clients.
  2. LoadCompletedCallback: The callback function after the user's personalized attributes are loaded.
  3. FailedCallback: The callback function when the user's personalized attributes fail to be loaded. The cause of failure may be network connection timeout or internal exceptions thrown by the user's personalized service.
  4. UserContext: Send the asynchronous call to the user context object on the server.

The four parameters listed in table 5-4 are optional. For the loadCompletedCallback and failedCallback parameters, if the default value is set for the ProfileService object (which will be introduced later), it can also be omitted.

The complete signature of the function specified by loadCompletedCallback is as follows (the name of the callback function and its parameters can be changed as needed ):

function onLoadCompleted(numProperties, userContext, methodName) 

ASP. net ajax provides three parameters when executing the callback:

  1. NumProperties: Indicates the number of user personalized attributes added during the loading process.
  2. UserContext: The user context object passed when the load () method is called.
  3. MethodName: Name of the method to call.

The complete signature of the callback function that fails to call the authentication service, that is, the function specified by the failedCallback parameter, is as follows (the name of the callback function and its parameters can be changed as needed ):

function onProfileFailed(error, userContext, methodName) 

ASP. net ajax also provides three parameters when executing callback:

  1. Error: Abnormal object that causes authentication service failure.
  2. UserContext: The user context object passed when the load () method is called.
  3. MethodName: Name of the method to call.

 

5.3.2Save ()Method

The save () method is used to save user personalized attributes that may be modified. The complete syntax for calling the save () method is as follows:

Sys.Services.ProfileService.save(
    propertyNames, 
    saveCompletedCallback, 
    failedCallback, 
    userContext
);

The meanings of parameters are shown in Table 5-5.

Table 5-5 parameters of the logout () method of the ProfileService object

  1. PropertyNames: An array containing string objects, indicating the set of user personalized attributes to be saved to the server. If this parameter is null, ASP. net ajax automatically saves all user-defined attributes that can be written from the client. This section describes how to selectively expose personalized attributes of users in ASP. NET 2.0 to clients.
  2. SaveCompletedCallback: Saves the callback function after the user's personalized attributes are completed.
  3. FailedCallback: Saves the callback function when the user's personalized attributes fail. The cause of failure may be network connection timeout or internal exceptions thrown by the user's personalized service.
  4. UserContext: Send the asynchronous call to the user context object on the server.

The four parameters listed in table 5-5 are optional. For the saveCompletedCallback and failedCallback parameters, if the default value is set for the ProfileService object (which will be introduced later), it can also be omitted.

The complete signature of the callback function after the user's personalized attributes are saved, that is, the function specified by the saveCompletedCallback parameter, is as follows (the name of the callback function and its parameters can be changed as needed ):

function onSaveCompleted(numProperties, userContext, methodName) 

ASP. net ajax provides three parameters when executing the callback:

  1. NumProperties: Indicates the number of user personalized attributes saved during the Save process.
  2. UserContext: The user context object passed when the save () method is called.
  3. MethodName: Name of the method to call.

The complete signature of the function specified by the failedCallback parameter is as follows (the name of the callback function and its parameters can be changed as needed ):

function onProfileFailed(error, userContext, methodName) 

ASP. net ajax also provides three parameters when executing callback:

  1. Error: Abnormal object that causes authentication service failure.
  2. UserContext: The user context object passed when the save () method is called.
  3. MethodName: Name of the method to call.

 

5.3.3PropertiesField

ASP. NET 2.0 provides a very powerful feature in user personalized application services. It allows developers to access predefined user personalized attributes in a strong way. For example, if the following user personalized attributes are defined in the <configuration/> \ <system. web/> section of the web. config file:

<profile enabled="true">
  <properties>
    <add name="Address" type="System.String" />
    <add name="Age" type="System.Int32" />
    <add name="InfoPanelPosition" type="System.Drawing.Point" />
  </properties>
</profile>

On the ASP. NET page, you can directly access these attributes in a strong type. Refer to the following section of server C # code:

string address = Profile.Address;
int age = Profile.Age;
System.Drawing.Point infoPanelPosition = Profile.InfoPanelPosition;

Although the JavaScript used on the client is a dynamic language, the variables are not so "strict" type constraints, however, the properties field provided by the ProfileService object allows developers to access user personalized attributes on the client using similar syntax rules.

After calling the load () method of the ProfileService object and successfully loading the user's personalized attributes, we can use its properties field to access personalized attributes of various users in JavaScript in a way similar to the above C # code;

var address = 
    Sys.Services.ProfileService.properties.Address;
var age = 
    Sys.Services.ProfileService.properties.Age;
var infoPanelPosition = 
    Sys.Services.ProfileService.properties.InfoPanelPosition;

The properties field of the ProfileService object can also support the user Profile Group defined in the web. config file ). For example:

<profile enabled="true">
  <properties>
    <group name="Address">
      <add name="Street" type="System.String" />
      <add name="City" type="System.String"/>
      <add name="PostalCode" type="System.String" />
    </group>
  </properties>
</profile>

In client JavaScript, you can also use the following syntax ([GroupName]. [PropertyName]) to access attributes in the user's personalized group:

var street = 
    Sys.Services.ProfileService.properties.Address.Street;
var city = 
    Sys.Services.ProfileService.properties.Address.City;
var postCode = 
    Sys.Services.ProfileService.properties.Address.PostCode;

 

5.3.4Common attributes

In addition to the load (), save () methods and properties attributes described earlier, the ProfileService object also provides several frequently used attributes, as shown in table 5-6.

Table 5-6 common attributes of a ProfileService object

  1. Timeout: Gets or sets the timeout time for loading/saving user personalized attributes, in milliseconds.
  2. DefaultLoadCompletedCallback: Gets or sets the default callback function after the user's personalized attributes are loaded.
  3. DefaultSaveCompletedCallback: Gets or sets the default callback function after saving the user's personalized attributes.
  4. DefaultFailedCallback: Gets or sets the default callback function when the user's personalized attributes fail to be loaded/saved.

The following code sets the timeout attribute of the ProfileService object:

Sys.Services.ProfileService.set_timeout(3000); 

If the defaultLoadCompletedCallback, defaultSaveCompletedCallback, and defaultFailedCallback attributes are preset, you do not have to specify the callback functions when calling the load () and save () Methods of the ProfileService object. For example, the following code sets the three attributes of the ProfileService object and defines the corresponding default callback function:

Sys.Services.ProfileService.set_defaultLoadCompletedCallback(onLoadCompleted);
Sys.Services.ProfileService.set_defaultSaveCompletedCallback(onSaveCompleted);
Sys.Services.ProfileService.set_defaultFailedCallback(onProfileFailed);
 
function onLoadCompleted(numProperties, userContext, methodName) {
    // ...
}
 
function onSaveCompleted(numProperties, userContext, methodName) {
    // ...
}
 
function onProfileFailed(error, userContext, methodName) {
    // ...
}

Then, we can directly use the following code to load/save users' personalized attributes. Is it very simple?

Sys.Services.ProfileService.load();
Sys.Services.ProfileService.save();
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.