Using Profile Information with ASP.NET AJAX
Introduction
The Microsoft ASP.NET 2.0 AJAX Extensions profile service enables you to use the ASP.NET profile application service in applications. You can access profile data by using ECMAScript (JavaScript) code that accesses a client representation of the profile.
Enabling the Profile Service
To use the profile service from script, you must enable the profile service by using the following element in the application's Web.config file. For more information, see Configuring ASP.NET AJAX.
<system.web.extensions>
<scripting>
<webServices
< profileService enabled="true" />
</webServices
</scripting>
</system.web.extensions>
By default, no profile properties are available. For each profile property that you want to make available in a client application, add the property name to the readAccessProperties attribute of the <profileService> element. Similarly, for each server profile property that can be set based on data sent from a client application, add the property name to the writeAccessProperties attribute. The following example shows how to expose individual properties.
<profileService enabled="true"
readAccessProperties="Backgroundcolor,Foregroundcolor"
writeAccessProperties=" Backgroundcolor,Foregroundcolor"/>
You define the profile properties in the <profile> section by using syntax like that in the following example. For grouped properties, use the <group> element.
<profile enabled="true">
<add name=" Backgroundcolor" type="System.String"
defaultValue="white" />
<add name=" Foregroundcolor" type="System.String"
defaultValue="black" />
<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>
Example
The following example shows how to use the profile service from client script. The example consists of an ASP.NET Web page that contains an ScriptManager control. When this control is included on the page, the AuthenticationService object is automatically available to any client script in the page.
The page contains a login button with an associated event handler named OnClickLogin. Code in the method calls the login method of the AuthenticationService object.
After you are logged in, the loginComplete callback function is called, which calls the load method of the ProfileService object to load the profile for the current user.
The profile information consists of background and foreground colors that you can set after you are logged in. The background and foreground colors are profile properties you must set in the Web.config file. To define these properties, add the following elements to the application's Web.config file:
<profile enabled="true">
<properties>
<add name="Backgroundcolor" type="System.String"
defaultValue="white"/>
<add name="Foregroundcolor" type="System.String"
defaultValue="black"/>
</properties>
</profile>
The example code provides asynchronous completed callback functions for the load and save methods. You can also add failed callback functions for the load and save methods.
note
Before you run the example, make sure that there is at least one user defined in the application's membership database. For information about how to create a user in the default SQL Server Express Edition database, see Using Forms Authentication.