Use application services and localization in ASP. NET Ajax (1): authenticationservice

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.

 

Implementing user management is a matter of consideration for almost every website. ASP. NET 2.0 has built an Identity Authentication application.ProgramService and provide it to developers in a unified manner. The Identity Authentication application service is not only easy to use, but also provides flexible scalability.

Reference: If you want to learn more about ASP. NET 2.0 identity authentication services, refer to the two msdnArticle: How ASP. NET security works (http://msdn2.microsoft.com/en-us/library/78d5caaf-055e-41f1-bbde-1b5020775edb.aspx) and managed users by using membership (http://msdn2.microsoft.com/en-us/library/824c3a24-f0af-427c-a652-0d2d1e9397cd.aspx ).

The ASP. Net Ajax Framework's identity authentication service can be integrated with ASP. NET 2.0's Identity Authentication application service, and provide it with the client's JavaScript call proxy. Some Identity Authentication Proxy functions of ASP. NET Ajax clients are provided by the client authenticationservice object.

The full name of the authenticationservice object is sys. Services. authenticationservice. This is an object in singleton mode and can be used without manual instance creation. As long as the page contains the scriptmanager control, we can directly access the authenticationservice object on the client, and then indirectly deal with the server identity authentication service and use ASP.. NET 2.0 provides identity authentication functions, such as user logon and logout-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 authenticationservice object provides two methods: Login () and logout (), which are used to implement user logon and logout respectively, and expose a series of common attributes. Next let's introduce them one by one:

5.1.1Login ()Method

The login () method is used to pass the user name and password to the server for verification and return whether the verification is successful. If necessary, the corresponding client cookie and other information will be set. The complete syntax for calling the login () method is as follows:

 
SYS. Services. authenticationservice. login (
 
Username,
 
Password,
 
Ispersistent,
 
Custominfo,
 
Redirecturl,
Logincompletedcallback,
 
Failedcallback,
 
Usercontext
 
);

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

Table 5-1 parameters of the authenticationservice object login () method

    1. Username: Username of the user to be authenticated.
    2. Password: Password of the user to be authenticated.
    3. Ispersistent: Boolean value, indicating whether the authentication information for persistent and cross-browser sessions is retained.
    4. Custominfo: Reserved field, which may be used in the future.
    5. Redirecturl: The URL to be redirected after the verification is successful. If the value of this parameter is null, no redirection will occur. The default value is null.
    6. Logincompletedcallback: Call the callback function after the authentication service is complete. Note that the callback function is called no matter whether the user has passed server-side authentication. Then, you can obtain the information about whether the user has passed the verification in the callback function.
    7. Failedcallback: Call the callback function after the authentication service fails. The cause of the failure may be network connection timeout or internal exceptions thrown by the authentication service. If the user name and password provided by the user are incorrect and cannot pass verification, the authentication service call will not fail and the callback function will not be called.
    8. Usercontext: Send the Asynchronous User Authentication call to the user context object on the server.

Of the eight parameters listed in table 5-1, only username, password, and ispersistent are required. For the logincompletedcallback and failedcallback parameters, if the default value is set for the authenticationservice object (which will be introduced later), it can also be omitted.

Call the callback function after the authentication service is complete, that is, the complete signature of the function specified by the logincompletedcallback parameter is as follows (the name of the callback function and its parameters can be changed as needed ):

Function onlogincompleted (validcredentials, usercontext, methodname)

ASP. NET Ajax provides three parameters when executing the callback:

    1. Validcredentials: A boolean value indicating whether the user has successfully passed the verification.
    2. Usercontext: The user context object passed when the login () 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 ):

 
FunctionOnauthenticationfailed (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 login () method is called.
    3. Methodname: Name of the method to call.

 

5.1.2Logout ()Method

The logout () method is used to log out a user that is currently logged on. The complete syntax for calling the logout () method is as follows:

SYS. Services. authenticationservice. logout (
 
Redirecturl,
 
Logoutcompletedcallback,
 
Failedcallback,
 
Usercontext
 
);

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

Table 5-2 parameters of the authenticationservice object logout () method

    1. Redirecturl: The URL to be redirected after the logout is successful. If the value of this parameter is null, it is automatically redirected to the current page. The default value is null.
    2. Logoutcompletedcallback: Deregister the callback function after the user completes.
    3. Failedcallback: Callback function after the user fails to be logged out. The cause of the failure may be network connection timeout or internal exceptions thrown by the authentication service.
    4. Usercontext: Send the Asynchronous User Authentication call to the user context object on the server.

The four parameters listed in Table 5-2 are optional. For the logoutcompletedcallback and failedcallback parameters, if the default value is set for the authenticationservice object (which will be introduced later), it can also be omitted.

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

FunctionOnlogoutcompleted (validcredentials, usercontext, methodname)

ASP. NET Ajax provides three parameters when executing the callback:

    1. Result: This parameter is not currently used and is always null.
    2. Usercontext: The user context object passed when the logout () 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 ):

 
FunctionOnauthenticationfailed (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 login () method is called.
    3. Methodname: Name of the method to call.

 

5.1.3Common attributes

In addition to the login () and logout () methods described earlier, the authenticationservice object also provides several frequently used attributes, as shown in table 5-3.

Table 5-3 common attributes of the authenticationservice object

    1. Isloggedin: Get a Boolean attribute, indicating whether the current user has logged on.
    2. Timeout: Gets or sets the authentication request timeout time, in milliseconds.
    3. Defaultlogincompletedcallback: Gets or sets the default callback function after the authentication service is called.
    4. Defaultlogoutcompletedcallback: Gets or sets the default callback function after logging out of the user.
    5. Defaultfailedcallback: Gets or sets the default callback function after logon/logout fails.

As follows:CodeDemonstrate setting the timeout attribute of the authenticationservice object:

 
SYS. Services. authenticationservice. set_timeout (3000 );

If the loglogincompletedcallback, defaultlogoutcompletedcallback, and defaultfailedcallback attributes are set in advance, the login () and logout () Methods of the authenticationservice object can be called without the work of specifying various callback functions. For example, the following code sets the three attributes of authenticationservice and defines the corresponding default callback function:

SYS. Services. authenticationservice. set_defaultlogincompletedcallback (onlogincompleted );
 
SYS. Services. authenticationservice. set_defaultlogoutcompletedcallback (onlogoutcompleted );
 
SYS. Services. authenticationservice. set_defaultfailedcallback (onauthenticationfailed );
 
 
 
FunctionOnlogincompleted (validcredentials, usercontext, methodname ){
 
//...
 
}
 
FunctionOnlogoutcompleted (result, usercontext, methodname ){
 
//...
 
}
 
FunctionOnauthenticationfailed (error, usercontext, methodname ){
//...
 
}

Then, we can directly use the following code to log on or log out. Username indicates the user name and password indicates the password:

 
SYS. Services. authenticationservice. login (username, password,True);
 
SYS. Services. authenticationservice. logout ();

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.