共用方式為


搭配 ASP.NET AJAX 使用角色資訊

更新:2007 年 11 月

ASP.NET 角色可讓您為已驗證的使用者分組,並將每一個群組視為一個單位。這個作法對適用於多位使用者的動作 (例如,允許或拒絕存取伺服器上的資源) 來說都很有用。

Microsoft AJAX Library 應用程式角色服務可讓您從已啟用 AJAX 功能之 ASP.NET Web 應用程式中的用戶端指令碼,存取已驗證之使用者的角色。您可以使用 Microsoft AJAX Library 應用程式角色服務,利用用戶端指令碼執行以角色為基礎的工作。執行網頁時,您可以呼叫 ECMAScript (JavaScript) RoleService Proxy 類別,它是由應用程式角色服務自動產生並下載至瀏覽器。

本主題將說明如何使用 JavaScript 從瀏覽器呼叫 ASP.NET 應用程式角色服務。

啟用角色服務

若要從用戶端指令碼使用角色服務,您必須在應用程式的 Web.config 檔案中使用下列項目以啟用這項服務。

<system.web.extensions>
  <scripting>
    <webServices
      < rolesService enabled="true" />
    </webServices
  </scripting>
</system.web.extensions>

如需詳細資訊,請參閱 HOW TO:設定 ASP.NET AJAX 中的 ASP.NET 服務

範例

下列範例會說明如何從用戶端指令碼使用角色服務。

注意事項:

若要執行範例,請確定應用程式的成員資格資料庫中至少已定義一個使用者。如需如何在預設 SQL Server Express Edition 資料庫中建立使用者的詳細資訊,請參閱搭配 ASP.NET AJAX 使用表單驗證

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" >

    <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 }
    </style>

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



</script>


</head>
<body>

    <form id="form1" >

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

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

        <hr />

        <table>
            <tr>
                <td align="left">User's roles: </td>
                <td align="left"> 
                    <input type="button" id="UserRoles" 
                           onclick="ReadUserRoles(); return false;" 
                           value="UserRoles" />
                </td>
            </tr>
            <tr>
                <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>
            </tr>
            
            <tr>
                <td align="left">Role Service path: </td>
                <td align="left">
                    <input type="button" id="ServicePathId" 
                           onclick="GetRoleServicePath(); return false;" 
                           value="Role Service Path" /></td>
            </tr>
            <tr>
                <td align="left">Role Service timeout: </td>
                <td align="left">
                    <input type="button" id="ServiceTimeoutId" 
                           onclick="GetRoleServiceTimeout(); return false;" 
                           value="Role Service Timeout" /></td>
            </tr>
                
        </table>              

    </form>

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

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" >

    <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 }
    </style>

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



</script>


</head>
<body>

    <form id="form1" >

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

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

        <hr />

        <table>
            <tr>
                <td align="left">User's roles: </td>
                <td align="left"> 
                    <input type="button" id="UserRoles" 
                           onclick="ReadUserRoles(); return false;" 
                           value="UserRoles" />
                </td>
            </tr>
            <tr>
                <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>
            </tr>
            
            <tr>
                <td align="left">Role Service path: </td>
                <td align="left">
                    <input type="button" id="ServicePathId" 
                           onclick="GetRoleServicePath(); return false;" 
                           value="Role Service Path" /></td>
            </tr>
            <tr>
                <td align="left">Role Service timeout: </td>
                <td align="left">
                    <input type="button" id="ServiceTimeoutId" 
                           onclick="GetRoleServiceTimeout(); return false;" 
                           value="Role Service Timeout" /></td>
            </tr>
                
        </table>              

    </form>

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

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

請參閱

工作

HOW TO:設定 ASP.NET AJAX 中的 ASP.NET 服務

概念

在 ASP.NET AJAX 中使用 Web 服務

從用戶端指令碼呼叫 Web 服務

搭配 ASP.NET AJAX 使用表單驗證

搭配 ASP.NET AJAX 使用設定檔資訊

Sys.Services.AuthenticationService 類別

Sys.Services.ProfileService 類別

Sys.Services.RoleService 類別