共用方式為


Sys.Services.RoleService Class

Provides a client proxy class for the ASP.NET role service.

Namespace: Sys.Services

Inherits: Sys.Net.WebServiceProxy

Members

Name

Description

Sys.Services.RoleService DefaultWebServicePath Field

Specifies the path to the default role service.

Sys.Services.RoleService Constructor

Initializes a new instance of the RoleService class.

Sys.Services RoleService load Method

Loads the roles for the currently authenticated user.

Sys.Services RoleService defaultFailedCallback Property

Gets or sets the name of the default failed callback function.

Sys.Services RoleService defaultLoadCompletedCallback Property

Gets or sets the name of the default load-completed callback function.

Sys.Services RoleService defaultSucceededCallback Property

Gets or sets the default succeeded callback function for the service.

Sys.Services RoleService defaultUserContext Property

Gets or sets the default user context for the service.

Sys.Services RoleService IsUserinRole Property

Checks whether the currently authenticated user is in the specified role.

Sys.Services RoleService path Property

Gets or sets the role service path.

Sys.Services RoleService roles Property

Gets the roles for the currently authenticated user.

Sys.Services RoleService timeout Property

Gets or sets the role service time-out value.

Remarks

The RoleService class provides script access to the currently authenticated user's roles information. The class calls methods of the role service through the same infrastructure that is used to call other Web service methods. The Web role service is implemented as a class that has only one instance (singleton). It is always available to your application through the RoleService proxy class and you do not have to instantiate it.

Example

Description

The following example shows how to use the RoleService class to obtain the roles for the currently authenticated user. For more information, see Using Roles Information with Microsoft Ajax.

Code

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.
    DefaultFailedCallBack();

    // Set the default load completed callback function.
    DefaultLoadCompletedCallBack()

}

// This function sets and gets the role service
// default failed callback function.
function DefaultFailedCallBack()
{
     // Set default failed callback function.
    roleProxy.set_defaultFailedCallback(FailedCallback);

    // Get the default callback function. 
    var failedCallBack =     
        roleProxy.get_defaultFailedCallback();

    alert(failedCallBack); 
}

// This function sets and gets the role service
// default load completed callback function.
function DefaultLoadCompletedCallBack()
{
    // Set the default callback function.
    roleProxy.set_defaultLoadCompletedCallback(LoadCompletedCallback);

    // Get the default callback function. 
    var loadCompletedCallBack =     
        roleProxy.get_defaultLoadCompletedCallback();

    alert(loadCompletedCallBack);
}

// This function checks if the currently
// authenticated user belongs to the
// passed role. 
function UserIsInRole(role) 
{
    DisplayInformation("");
    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 = 
        Sys.Services.RoleService.get_path();
    if (roleServicePath == "")
    {
        DisplayInformation(
            "The role service path is the default value.");
    }
}

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

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

    // 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 = 
        Sys.Services.RoleService.get_timeout();

    DisplayInformation(
            "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.
    roles.sort();
    for(var i = 0; i < roles.length; i++) 
    {
        var roleInfo = "Role: " + roles[i];
        DisplayInformation(roleInfo);
    }       
}

// 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();

See Also

Concepts

Sys.Services.AuthenticationService Class

Sys.Services.ProfileService Class

Using Roles Information with Microsoft Ajax