更新:2007 年 11 月
为 ASP.NET 角色服务提供客户端代理类。
命名空间:Sys.Services
成员
名称 |
说明 |
|---|---|
指定默认角色服务的路径。 |
|
初始化 RoleService 类的新实例。 |
|
加载当前经过身份验证的用户的角色。 |
|
获取或设置默认失败回调函数的名称。 |
|
获取或设置默认加载完成回调函数的名称。 |
|
获取或设置服务的默认成功回调函数。 |
|
获取或设置服务的默认用户上下文。 |
|
检查当前经过身份验证的用户是否具有指定的角色。 |
|
获取或设置角色服务路径。 |
|
获取当前经过身份验证的用户的角色。 |
|
获取或设置角色服务超时值。 |
备注
RoleService 类提供了对当前经过身份验证的用户角色信息的脚本访问方式。该类通过用于调用其他 Web 服务方法的相同基础结构来调用角色服务的方法。Web 角色服务被实现为仅具有一个实例的类(单一实例类)。应用程序始终可通过 RoleService 代理类使用该类,不必将其实例化。
示例
说明
下面的示例演示如何使用 RoleService 类获取当前经过身份验证的用户的角色。有关更多信息,请参见通过 ASP.NET AJAX 使用角色信息。
代码
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();