Use application services and localization in ASP. NET Ajax (5): Customize the server-side implementation of application services

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.

Identity Authentication and User PersonalizationProgramThe use and implementation methods of services in actual projects are extremely variable. Therefore, ASP. NET 2.0 provides powerful customization capabilities in the form of custom providers.

Reference: If you want to learn more about how to customize identity authentication and user personalized application service through custom providers in ASP. NET 2.0, refer to the msdnArticle: Implementing a membership provider (http://msdn2.microsoft.com/en-us/library/f1kyba5e.aspx) and implementing a profile provider (http://msdn2.microsoft.com/en-us/library/0580x1f5 (vs.80). aspx ).

In ASP. NET Ajax, application services often have the same custom requirements. Therefore, Asp. net Ajax provides a set of default proxy for asynchronous communication with server-side application services, while also providing full customization capabilities, this allows us to change the implementation of server-side application services as needed.

This section describes how to customize identity authentication and user personalized Application Service in ASP. NET Ajax.

5.5.1Custom Identity AuthenticationWeb Service

To customize the identity authentication service, we need to manually compile a web service, which must contain two methods: Login () and logout (), which are used for user login and logout respectively. The signatures of these two methods (including the method name, number of parameters, Parameter order, parameter type, and return value type) must meet certain requirements.

BelowCodeIs a prototype of custom Identity Authentication Web service. We can refer to the methods and method signatures provided in the prototype, or implement the identity authentication method suitable for the actual project based on the prototype:

 
[WebService (namespace ="Http://tempuri.org /")]
 
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
 
[System. Web. Script. Services. scriptservice]
 
Public ClassCustomauthenticationservice: system. Web. Services. WebService
 
{
 
/// <Summary>
 
/// Implement User Logon. You can customize the authentication logic.
 
/// </Summary>
/// <Param name = "username"> User name to be verified </param>
 
/// <Param name = "password"> User Password to be verified </param>
 
/// <Param name = "createpersistentcookie"> whether to create a cookie to ensure that the logon information exists in a cross-browser session </param>
 
/// <Returns> Boolean value, indicating whether the verification is successful </returns>
 
[Webmethod]
 
Public BoolLogin (StringUsername,StringPassword,BoolCreatepersistentcookie)
 
{
 
Return True;
 
}
 
 
/// <Summary>
 
/// Implement User cancellation.
 
/// </Summary>
 
[Webmethod]
 
Public VoidLogout ()
 
{
 
}
 
}

After the custom Identity Authentication Web service is completed, to use this custom Identity Authentication Service on an ASP. NET Ajax page, we need to explicitly declare in scriptmanager:

<ASP: scriptmanager ID= "SM" Runat= "Server">
 
<Authenticationservice 
 
Path= "~ /Customapplicationservices/customauthenticationservice. asmx" />
 
</ASP: scriptmanager>

The <authenticationservice/> label in the bold part corresponds to the system. Web. UI. authenticationservicemanager type of the ASP. NET Ajax server, and is responsible for setting the location of the custom web service for identity authentication on the page. After you set the path attribute to the previously written customauthenticationservice. asmx web service, the identity authentication process on this page will be processed by the custom customauthenticationservice. asmx.

Tip: If you set the path attribute of the <authenticationservice/> label to an empty string or do not set this attribute, ASP. NET Ajax will still use the default authentication service.

 

5.5.2Customize User PersonalizationWeb Service

Like the User-Defined identity authentication service, if you want to customize the user's personalized service, we also need to strictly follow certain rules (fixed number of methods and method signatures) write a Web Service manually. Here we provide a prototype of custom web service. The specific implementation method needs to be analyzed according to the actual project requirements:

 
[WebService (namespace ="Http://tempuri.org /")]
 
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
 
[System. Web. Script. Services. scriptservice]
 
Public ClassCustomprofileservice: system. Web. Services. WebService
 
{
 
/// <Summary>
/// Return all personalized attributes of the current user
 
/// </Summary>
 
/// <Returns> idictionary containing all the personalized attributes of the current user </returns>
 
[Webmethod]
 
PublicIdictionary <String,Object> Getallpropertiesforcurrentuser ()
 
{
 
Return Null;
 
}
 
 
 
/// <Summary>
 
/// Return the specified personalized attributes of the current user
 
/// </Summary>
/// <Param name = "properties"> array containing the personalized attribute name to be obtained </param>
 
/// <Returns> idictionary containing the personalized attributes specified by the current user </returns>
 
[Webmethod]
 
PublicIdictionary <String,Object> Getpropertiesforcurrentuser (String[] Properties)
 
{
 
Return Null;
 
}
 
 
 
/// <Summary>
 
/// Set the custom attributes of the current user
 
/// </Summary>
/// <Param name = "values"> idictionary containing the personalized attributes specified by the current user </param>
 
/// <Returns> Number of user personalized attributes successfully updated </returns>
 
[Webmethod]
 
Public IntSetpropertiesforcurrentuser (idictionary <String,Object> Values)
 
{
 
Return0;
 
}
 
}

After you customize your web service, if you want to use this Custom User personalized service on an ASP. NET Ajax page, you also need to explicitly declare in scriptmanager:

<ASP: scriptmanager ID= "SM" Runat= "Server">
 
<Profileservice
 
Path= "~ /Customapplicationservices/customprofileservice. asmx" />
 
</ASP: scriptmanager>

The <profileservice/> label in the bold part corresponds to the system. Web. UI. profileservicemanager type of the ASP. NET Ajax server, and is responsible for setting the custom web service location on the page. After you set the path attribute to the previously written customprofileservice. asmx web service, the user personalized service on this page will be processed by the custom customprofileservice. asmx.

Tip: If you set the path attribute of the <profileservice/> label to an empty string or do not set this attribute, ASP. NET Ajax will still use the default user personalized service.

The profileservicemanager type also exposes a property: loadproperties, indicating the user-defined attributes that you want to send to the client along with the loading of the page HTML code. We can set multiple personalized attributes for loadproperties, separated by commas.

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.