次の方法で共有


ASP.NET AJAX でのプロファイル情報の使用

更新 : 2007 年 11 月

ASP.NET プロファイル Web サービスにより、 アプリケーションで ASP.NET プロファイル アプリケーション サービスを使用できます。このトピックでは、ECMAScript (JavaScript) コードを使用してブラウザからアプリケーション プロファイル サービスを呼び出す方法を説明します。

プロファイル情報を使用するには、最初にプロファイル サービスを Web サイトで有効にする必要があります。このためには、プロファイル サービスを Web サービスとして使用できるようにします。プロファイル Web サービスが有効になった場合、クライアント スクリプトで Sys.Services.ProfileService クラスのメソッドを呼び出すことで、プロファイル Web サービスを呼び出すことができます。

プロファイル サービスの有効化

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

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

既定では、プロファイル プロパティは使用できません。クライアント アプリケーションで使用できるようにする各プロファイル プロパティについて、そのプロパティ名を profileService 要素の readAccessProperties 属性に追加します。同様に、クライアント アプリケーションから送信されるデータに基づいて設定できるようにする各サーバー プロファイル プロパティについて、そのプロパティ名を writeAccessProperties 属性に追加します。個々のプロパティを公開する方法、およびクライアント アプリケーションがそれらのプロパティを読み込むこと、および書き込むことができるようにするかどうか設定する方法を、次の例に示します。

<profileService enabled="true" 
  readAccessProperties="Backgroundcolor,Foregroundcolor" 
  writeAccessProperties=" Backgroundcolor,Foregroundcolor"/>

次の例の構文と同様の構文を使用して、profile セクションでプロファイル プロパティを定義します。グループ化されたプロパティの場合は、group 要素を使用します。

<system.web>
  <profile enabled="true">
    <add name=" Backgroundcolor" type="System.String"
       defaultValue="white" />
    <add name=" Foregroundcolor" type="System.String"
     defaultValue="black" />
    <properties>
      <group name="Address">
       <add name="Street" type="System.String" />
       <add name="City" type="System.String"/>
       <add name="PostalCode" type="System.String" />
      </group>
    </properties>
  </profile>
</system.web>

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

クライアント スクリプトからプロファイル サービスを使用する方法を次の例に示します。この例は、ScriptManager コントロールを含む ASP.NET Web ページで構成されています。ページにこのコントロールが含まれている場合、ページ内のクライアント スクリプトで Sys.Services.AuthenticationService オブジェクトを自動的に使用できるようになります。

ページには、OnClickLogin という名前のイベント ハンドラに関連付けられたログイン ボタンが含まれます。このメソッドのコードにより、AuthenticationService オブジェクトの login メソッドが呼び出されます。

ログイン後、loginComplete コールバック関数が呼び出されます。この関数は、現在のユーザーのプロファイルを読み込む、Sys.Services.ProfileService オブジェクトの load メソッドを呼び出します。

プロファイル情報には背景色と前景色があり、ログイン後に設定できます。背景色および前景色は、Web.config ファイルで設定する必要のあるプロファイル プロパティです。これらのプロパティを定義するには、アプリケーションの Web.config ファイルに次の要素を追加します。

<profile enabled="true">
  <properties>
    <add name="Backgroundcolor" type="System.String"
       defaultValue="white"/>
    <add name="Foregroundcolor" type="System.String"
       defaultValue="black"/>
  </properties>
</profile>

例のコードでは、非同期に実行される、読み込みメソッドおよび保存メソッドに関するコールバック関数が用意されています。また、読み込みメソッドおよび保存メソッドに関する、失敗した場合のコールバック関数を追加することもできます。

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

例を実行する前に、1 人以上のユーザーがアプリケーションのメンバシップ データベースで定義されていることを確認してください。既定の SQL Server Express Edition データベースにユーザーを作成する方法については、「ASP.NET AJAX でのフォーム認証の使用」を参照してください。

var setProfileProps;

function pageLoad()
{
    var userLoggedIn = 
        Sys.Services.AuthenticationService.get_isLoggedIn();
        // alert(userLoggedIn);
        
    profProperties = $get("setProfileProps");
    passwordEntry = $get("PwdId");
    
    if (userLoggedIn == true)
    {
        LoadProfile();
        GetElementById("setProfProps").style.visibility = "visible";
        GetElementById("logoutId").style.visibility = "visible";
    }
    else
    {
        DisplayInformation("User is not authenticated.");
    }
     
}


// The OnClickLogout function is called when 
// the user clicks the Logout button. 
// It logs out the current authenticated user.
function OnClickLogout()
{
    Sys.Services.AuthenticationService.logout(
        null, OnLogoutComplete, AuthenticationFailedCallback,null);
}


function OnLogoutComplete(result, 
    userContext, methodName)
{
    // Code that performs logout 
    // housekeeping goes here.          
}       



// This is the callback function called 
// if the authentication failed.
function AuthenticationFailedCallback(error_object, 
    userContext, methodName)
{   
    DisplayInformation("Authentication failed with this error: " +
        error_object.get_message());
}



// Loads the profile of the current
// authenticated user.
function LoadProfile()
{
    Sys.Services.ProfileService.load(null, 
        LoadCompletedCallback, ProfileFailedCallback, null);

}

// Saves the new profile
// information entered by the user.
function SaveProfile()
{
    
    // Set background color.
    Sys.Services.ProfileService.properties.Backgroundcolor = 
        GetElementById("bgcolor").value;
    
    // Set foreground color.
    Sys.Services.ProfileService.properties.Foregroundcolor =
        GetElementById("fgcolor").value; 
    
    // Save profile information.
    Sys.Services.ProfileService.save(null, 
        SaveCompletedCallback, ProfileFailedCallback, null);
    
}

// Reads the profile information and displays it.
function LoadCompletedCallback(numProperties, userContext, methodName)
{
    document.bgColor = 
        Sys.Services.ProfileService.properties.Backgroundcolor;

    document.fgColor =   
        Sys.Services.ProfileService.properties.Foregroundcolor;         
}

// This is the callback function called 
// if the profile was saved successfully.
function SaveCompletedCallback(numProperties, userContext, methodName)
{
    LoadProfile();
    // Hide the area that contains 
    // the controls to set the profile properties.
    SetProfileControlsVisibility("hidden");
}

// This is the callback function called 
// if the profile load or save operations failed.
function ProfileFailedCallback(error_object, userContext, methodName)
{
    alert("Profile service failed with message: " + 
            error_object.get_message());
}


// Utility functions.

// This function sets the visibilty for the
// area containing the page elements for settings
// profiles.
function SetProfileControlsVisibility(currentVisibility)
{
   profProperties.style.visibility = currentVisibility; 
}

// Utility function to display user's information.
function DisplayInformation(text)
{
    document.getElementById('placeHolder').innerHTML += 
    "<br/>"+ text;
}


function GetElementById(elementId)
{
    var element = document.getElementById(elementId);
    return element;
}

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

<%@ Page Language="VB" %>

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

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

        .text { font: 8pt Trebuchet MS }
    </style>

</head>
<body>

    <form id="form1" runat="server">
    
        <asp:ScriptManager runat="server" ID="ScriptManagerId">
            <Scripts>
                <asp:ScriptReference Path="Profile.js" />
            </Scripts>
        </asp:ScriptManager>


        <h2>Profile Service</h2>
    
        <p>
            You must log in first to set or get profile data. 
            Create a new user, if needed. <br /> Refer to the Authentication example.
        </p>
         
        <div id="loginId" style="visibility:visible">
            <table id="loginForm">
                <tr>
                    <td>User Name: </td>
                    <td><input type="text" 
                        id="userId" name="userId" value=""/></td>
                </tr>
                
                <tr>
                    <td>Password: </td>
                    <td><input type="password" 
                        id="userPwd" name="userPwd" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                        id="login" name="login" value="Login" 
                            onclick="OnClickLogin()" /></td>
                </tr>
            </table>              
    
        </div>
        
        <div id="setProfProps" style="visibility:hidden">
            <input type="button" 
            value="Set Profile Properties" 
            onclick="SetProfileControlsVisibility('visible')"/> 
        </div>
        
        <div id="placeHolder" style="visibility:visible" />
        
        <br />
        
        
        <input id="logoutId" type="button" 
            value="Logout"  style="visibility:hidden"
        onclick="OnClickLogout()" />
        
    
        <div id="setProfileProps" style="visibility:hidden">
            <table>
                <tr>
                    <td>Foreground Color</td>
                    <td><input type="text" id="fgcolor" 
                    name="fgcolor" value=""/></td>
                </tr>
                
                <tr>
                    <td>Background Color</td>
                    <td><input type="text" id="bgcolor" 
                        name="bgcolor" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                    id="saveProf" name="saveProf" 
                    value="Save Profile Properties" 
                    onclick="SaveProfile();" /></td>
                </tr>
            </table>      
        </div>        
    
    </form>

</body>

</html>
<%@ Page Language="C#" %>

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

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

        .text { font: 8pt Trebuchet MS }
    </style>

</head>
<body>

    <form id="form1" runat="server">
    
        <asp:ScriptManager runat="server" ID="ScriptManagerId">
            <Scripts>
                <asp:ScriptReference Path="Profile.js" />
            </Scripts>
        </asp:ScriptManager>


        <h2>Profile Service</h2>
    
        <p>
            You must log in first to set or get profile data. 
            Create a new user, if needed. <br /> 
        </p>
         
        <div id="setProfProps" style="visibility:visible">
            <input type="button" 
            value="Set Profile Properties" 
            onclick="SetProfileControlsVisibility('visible')"/> 
        </div>
        
        <div id="placeHolder" style="visibility:visible" />
        
        <br />
        
        
        <input id="logoutId" type="button" 
            value="Logout"  style="visibility:hidden"
        onclick="OnClickLogout()" />
        
    
        <div id="setProfileProps" style="visibility:hidden">
            <table>
                <tr>
                    <td>Foreground Color</td>
                    <td><input type="text" id="fgcolor" 
                    name="fgcolor" value=""/></td>
                </tr>
                
                <tr>
                    <td>Background Color</td>
                    <td><input type="text" id="bgcolor" 
                        name="bgcolor" value="" /></td>
                </tr>
                
                <tr>
                    <td><input type="button" 
                    id="saveProf" name="saveProf" 
                    value="Save Profile Properties" 
                    onclick="SaveProfile();" /></td>
                </tr>
            </table>      
        </div>        
    
    </form>

</body>

</html>

参照

処理手順

方法 : ASP.NET AJAX で ASP.NET サービスを構成する

概念

ASP.NET AJAX での Web サービスの使用

クライアント スクリプトからの Web サービスの呼び出し

ASP.NET AJAX でのフォーム認証の使用

Sys.Services.AuthenticationService クラス

Sys.Services.ProfileService クラス