ASP.NET AJAX Application Services

In this post, we’ll examine the built-in services in ASP.NET AJAX. You call these services in a same way as you’d call any custom web service from script, but there are some differences.

There are three built-in services in ASP.NET AJAX, namely:

  • Authentication service
  • Role service
  • Profile service

To use these services, you must take two steps: first, configure that you’d like to call these services in your web.config file, then call the configured services from script in your pages. Both the configuration and the calling code is different for the three services, so lets get started with authentication.

Authentication service

Authentication service gives you the ability to perform the login process of a client without requiring a full postback. This is useful if you’re working with UpdatePanels, because you cannot use a Login control in an UpdatePanel effectively, since it requires a full postback. Also, be notified here that the authentication service supports only cookie-based authentication (you should know it for the exam).

The setup of the authentication service is by all terms easy. First, you must set up your web page to use forms authentication, then you need to enable the authentication service. Here’s the code for both of the operations:

    <authentication mode=”Forms” />
            <authenticationService enabled=”true”/>

Voilá. You can optionally set the requireSSL property, which does what its name tells you: only uses the authentication service when Secure Socket Layers are initialized. There are three methods of the authentication service that you’ll find quite useful:

  • login: is a bit complex, here’s the parameter list: login(userName, password, isPersistent, redirectUrl, customInfo, loginCompletedCallback, failCallback, userContext)
    • userName, password need no further explanation
    • isPersistent means whether or not the authentication cookie should be persisted between browser sessions
    • redirectUrl: if the login succeeds, user would be redirected here
    • customInfo: reserved for future use, just pass a null value
    • loginCompletedCallback if you do not specify a redirectUrl, or pass an empty string, the function marked here will execute
    • failCallback the function to call when login fails
  • logout: logs the current user out (removes the authentication cookie). You can specify callbacks here, too.
  • get_isLoggedIn: returns true if the current user is logged in.

Now an example of this in code:

var userName = $get(“userNameBox”);
var password = $get(“passwordBox”);
Sys.Services.AuthenticationServie.login(userName, password, false, null, null, null, loginSuccess, loginFails, null);
function loginSuccess (validCredentials, userContext, methodName) {
if(validCredentials == false) {
    $get(“statusLabel”).innerHTML = “Failed login!”;
else {
    $get(“statusLabel”).innerHTML = “Welcome to the jungle!”;
function loginFails(error, userContext, methodName) {

You should be aware of that when you work with services in script, you need to define callbacks for get and work with the result. The authentication service is a bit special, because you can add an url to the parameter list, so in a manner, it doesn’t require callbacks written by you.

But you should keep in mind that callbacks are needed for these services. Services sits on the server, and you’re querying them from code that’s executing on the client. So if someone ask a question (probably on an exam), always choose the one with the callback.

Role service

You can turn on the role service just in the way you did it with authentication service. The astonishing difference is that this time, you need to set the enabled attribute of the roleService tag. The methods of role service (which can be invoked through Sys.Services.RoleService are as follows:

  • load: returns the roles for the given user. You must call this method before you begin working with role service. Also, you must supply callbacks for success and for failure. The method signature is: load(loadCompletedCallback, failedCallback, userContext).
  • isUserInRole: returns true if the current user is the member of the indicated role.
  • get_roles: returns a string array representing the roles the current user is assigned too.

An example:

Sys.Services.RoleService.load(roleLoad, roleFail, null);
function roleLoad(result, userContext, methodName) {
            $get(“statLabel”).innerHTML = “Admin’s here!”;
function roleFail(error, userContext, methodName) {

Before calling the load method, make sure the current user is authenticated (Windows or Forms), because when the user isn’t authenticated, no roles would be returned (but the process would be indicated as successful).

Profile service

To turn on profile service, you need to set the enabled attribute of the profileService tag to true, but this isn’t enough for actually working with profiles. You need to include a comma-delimited list of the profile properties you wish to let the service modify (writeAccessProperties) or just read (readAccessProperties). Naturally you would need a properly configured profile section in your web.config.

Sys.Services.ProfileService has three methods you’d love:

  • load: retrieves profile properties listed for the current user. You must call this method before starting to work with the profile service, and you’ll need to include callback methods for retrieving the result. The parameter list: load(propertyNames, onSuccess, onFail, userContext).
  • properties: a collection of profile data to work with. You access the properties exposed to the profile service like the following syntax:
  • save: passes the profile data back to the server, where it’ll be updated.

Further Readings

Sys.Services.AuthenticationService login Method

Sys.Services RoleService load Method


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: