ASP.NET AJAX でのロール情報の使用

更新 : 2007 年 11 月

ASP.NET のロールを使用すると、認証されたユーザーをグループ化して、各グループを単位として扱うことができます。この機能は、サーバー上のリソースへのアクセスを有効または無効にする場合など、アクションを複数のユーザーに対して適用する場合に役立ちます。

Microsoft AJAX Library アプリケーションのロール サービスを利用すると、AJAX 対応の ASP.NET Web アプリケーションのクライアント スクリプトから、認証されたユーザーのロールにアクセスできます。クライアント スクリプトを使ったロール ベース タスクの実行に Microsoft AJAX Library アプリケーションのロール サービスを利用することもできます。ページを実行すると、ECMAScript (JavaScript) の RoleService プロキシ クラスを呼び出すことができます。このプロキシ クラスはアプリケーション ロール サービスによって自動的に生成され、ブラウザにダウンロードされます。

このトピックでは、JavaScript を使用してブラウザから ASP.NET のアプリケーション ロール サービスを呼び出す方法を説明します。

ロール サービスの有効化

クライアント スクリプトからロール サービスを使用するには、アプリケーションの Web.config ファイルで次の要素を使用して、ロール サービスを有効にする必要があります。

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

詳細については、「方法 : ASP.NET AJAX で ASP.NET サービスを構成する」を参照してください。

クライアント スクリプトからロール サービスを使用する方法を次の例に示します。

Bb514828.alert_note(ja-jp,VS.90).gifメモ :

例を実行するには、1 人以上のユーザーがアプリケーションのメンバシップ データベースで定義されていることを確認してください。既定の 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" 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 }
    </style>

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



</script>


</head>
<body>

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

        <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 runat="server" 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" 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 }
    </style>

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



</script>


</head>
<body>

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

        <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 runat="server" 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();

参照

処理手順

方法 : 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 クラス