Using Roles Information with Microsoft Ajax

ASP.NET roles enable you to group authenticated users and to treat each group as a unit. This can be useful for any action that applies to multiple users, such as enabling or denying access to resources on the server.

The Microsoft Ajax Library application role service enables you to access an authenticated user's roles from client script in an AJAX-enabled ASP.NET Web application. You can use the Microsoft Ajax Library application role service to perform role-based tasks by using client script. When the page runs, you can call the ECMAScript (JavaScript) RoleService proxy class, which is automatically generated by the application role service and downloaded to the browser.

This topic shows how to call the ASP.NET application role service from the browser by using JavaScript.

To use the role service from client script, you must enable it by using the following element in the application's Web.config file.

      < rolesService enabled="true" />

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

The following example shows how to use the role service from client script.

Note Note

To 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 Microsoft Ajax.

<%@ Page Language="C#"  Title="Using Roles Service" %>

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

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

        .text { font: 8pt Trebuchet MS }

<script language="javascript" type="text/jscript">



    <form id="form1" runat="server">

        <h2>Access Authenticated User's Roles</h2>
	    Using the Sys.Services.RoleService client type to obtain roles information.

	        You must be authenticated to access roles information. <br /> 
	        Refer to the Forms Authentication example.
	   <asp:ScriptManager runat="server" ID="ScriptManagerId">
                <asp:ScriptReference Path="Roles.js" />

        <hr />
			    <td align="left">User's roles: </td>
			    <td align="left"> 
			        <input type="button" id="UserRoles" 
					       onclick="ReadUserRoles(); return false;" 
					       value="UserRoles" />
			    <td align="left">Is User in Administrators Role?: </td>
				<td align="left" >
				    <input type="button" id="UserInRole" 
					       onclick="UserIsInRole('Administrators'); return false;" 
					       value="Check Role" /></td>
			    <td align="left">Role Service path: </td>
				<td align="left">
				    <input type="button" id="ServicePathId" 
					       onclick="GetRoleServicePath(); return false;" 
					       value="Role Service Path" /></td>

			    <td align="left">Role Service timeout: </td>
				<td align="left">
				    <input type="button" id="ServiceTimeoutId" 
					       onclick="GetRoleServiceTimeout(); return false;" 
					       value="Role Service Timeout" /></td>


    <div id="placeHolder" ></div>


var roleProxy;
var roles;

// This function creates the role proxy and 
// sets its default callback functions.
function pageLoad()
    // Create role service proxy.
    roleProxy = Sys.Services.RoleService;

    // Set the default failed callback function.

    // Set the default load completed callback function.


// This function sets and gets the role service
// default failed callback function.
function DefaultFailedCallBack()
     // Set default failed callback function.

    // Get the default callback function. 
    var failedCallBack =     


// This function sets and gets the role service
// default load completed callback function.
function DefaultLoadCompletedCallBack()
    // Set the default callback function.

    // Get the default callback function. 
    var loadCompletedCallBack =     


// This function checks if the currently
// authenticated user belongs to the
// passed role. 
function UserIsInRole(role) 
    var isUserInRole = roleProxy.isUserInRole(role);
    DisplayInformation("The user is in the " + role + 
        " role: " + isUserInRole);   

// This function gets the role service path.
function GetRoleServicePath()
    // Get the role service path.
    var roleServicePath = 
    if (roleServicePath == "")
            "The role service path is the default value.");

// This function gets the roles of the
// currently authenticated user. 
function ReadUserRoles() 
    // Clear the feedback output.

    // You must load the user's roles 
    // first before you can use them.

    // Read the user's roles. 
    roles = roleProxy.get_roles();


// This function gets the role service timeout.
function GetRoleServiceTimeout()
    // Get the role service path.
    var roleServiceTimeout = 

            "Role service timeout: " + roleServiceTimeout);


// This is the callback function
// called if the role service load function
// completed.
function LoadCompletedCallback(roles) 
    // Read the user's roles loaded
    // before.
    for(var i = 0; i < roles.length; i++) 
        var roleInfo = "Role: " + roles[i];

// This is the callback function
// called if the role service fails.
function FailedCallback(error) 
    DisplayInformation("Error: " + error.get_message());   

// This function displays user's 
// feedback information.
function DisplayInformation(text)
	document.getElementById('placeHolder').innerHTML = 
	"<br/>"+ text;

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