This documentation is archived and is not being maintained.

Using Profile Information with ASP.NET AJAX

The ASP.NET profile Web service enables you to use the ASP.NET profile application service in  applications. This topic shows how to call the application profile service from the browser by using ECMAScript (JavaScript) code.

Using profile information requires that you first enable the profile service in the Web site. This makes it available as a Web service. When the profile Web service is enabled, you can invoke it by calling methods of the client-script Sys.Services.ProfileService class.

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.

      < profileService enabled="true" />

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 and set whether a client application can read and write them.

<profileService enabled="true" 
  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" />
      <group name="Address">
       <add name="Street" type="System.String" />
       <add name="City" type="System.String"/>
       <add name="PostalCode" type="System.String" />

For more information, see How to: Configure ASP.NET Services in ASP.NET AJAX.

The following example shows how to use the profile service from client script. The example consists of an ASP.NET Web page that contains a ScriptManager control. When this control is included on the page, the Sys.Services.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 Sys.Services.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">
    <add name="Backgroundcolor" type="System.String"
    <add name="Foregroundcolor" type="System.String"

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.


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 with ASP.NET AJAX.

var setProfileProps;

function pageLoad()
    var userLoggedIn = 
	    // alert(userLoggedIn);
	profProperties = $get("setProfileProps");
	passwordEntry = $get("PwdId");
	if (userLoggedIn == true)
        GetElementById("setProfProps").style.visibility = "visible";
	    GetElementById("logoutId").style.visibility = "visible";
	    DisplayInformation("User is not authenticated.");

// The OnClickLogout function is called when 
// the user clicks the Logout button. 
// It logs out the current authenticated user.
function OnClickLogout()
	    null, OnLogoutComplete, AuthenticationFailedCallback,null);

function OnLogoutComplete(result, 
    userContext, methodName)
	// Code that performs logout 
	// housekeeping goes here.			

// This is the callback function called 
// if the authentication failed.
function AuthenticationFailedCallback(error_object, 
    userContext, methodName)
    DisplayInformation("Authentication failed with this error: " +

// Loads the profile of the current
// authenticated user.
function LoadProfile()
	    LoadCompletedCallback, ProfileFailedCallback, null);


// Saves the new profile
// information entered by the user.
function SaveProfile()
	// Set background color. = 
	// Set foreground color. =
	// Save profile information., 
	    SaveCompletedCallback, ProfileFailedCallback, null);

// Reads the profile information and displays it.
function LoadCompletedCallback(numProperties, userContext, methodName)
	document.bgColor =;

    document.fgColor =;			

// This is the callback function called 
// if the profile was saved successfully.
function SaveCompletedCallback(numProperties, userContext, methodName)
	// Hide the area that contains 
	// the controls to set the profile properties.

// This is the callback function called 
// if the profile load or save operations failed.
function ProfileFailedCallback(error_object, userContext, methodName)
	alert("Profile service failed with message: " + 

// Utility functions.

// This function sets the visibilty for the
// area containing the page elements for settings
// profiles.
function SetProfileControlsVisibility(currentVisibility)
{ = currentVisibility; 

// Utility function to display user's information.
function DisplayInformation(text)
	document.getElementById('placeHolder').innerHTML += 
	"<br/>"+ text;

function GetElementById(elementId)
    var element = document.getElementById(elementId);
    return element;

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

<%@ Page Language="C#" %>

<html xmlns="" >
<head runat="server">

    <title>Using Profile Service</title>
   <style type="text/css">
        body {  font: 11pt Trebuchet MS;
                padding-top: 72px;
                text-align: center }

        .text { font: 8pt Trebuchet MS }


    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManagerId">
                <asp:ScriptReference Path="Profile.js" />

        <h2>Profile Service</h2>
	        You must log in first to set or get profile data. 
	        Create a new user, if needed. <br /> 
		<div id="setProfProps" style="visibility:visible">
			<input type="button" 
			value="Set Profile Properties" 
		<div id="placeHolder" style="visibility:visible" />
		<br />
	    <input id="logoutId" type="button" 
	        value="Logout"  style="visibility:hidden"
	    onclick="OnClickLogout()" />
		<div id="setProfileProps" style="visibility:hidden">
					<td>Foreground Color</td>
					<td><input type="text" id="fgcolor" 
					name="fgcolor" value=""/></td>
					<td>Background Color</td>
					<td><input type="text" id="bgcolor" 
					    name="bgcolor" value="" /></td>
					<td><input type="button" 
					id="saveProf" name="saveProf" 
					value="Save Profile Properties" 
					onclick="SaveProfile();" /></td>