Condividi tramite


Utilizzo delle informazioni sui ruoli con ASP.NET AJAX

Aggiornamento: novembre 2007

I ruoli ASP.NET consentono di raggruppare utenti autenticati e trattare ogni gruppo come unità. Questo procedimento può rivelarsi utile nel caso di azioni che vengono applicate a più utenti, ad esempio la concessione o negazione dell'accesso alle risorse nel server.

Il servizio ruolo dell'applicazione°Microsoft AJAX Library consente di accedere ai ruoli di un utente autenticato da uno script client in un'applicazione Web ASP.NET con supporto AJAX. È possibile utilizzare tale servizio per eseguire attività basate sui ruoli tramite script client. Quando viene eseguita la pagina, è possibile chiamare la°classe proxy RoleService°ECMAScript (JavaScript), generata automaticamente dal servizio ruolo dell'applicazione e scaricata nel browser.

In questo argomento viene illustrato come chiamare il servizio ruolo dell'applicazione ASP.NET dal browser tramite JavaScript.

Attivazione del servizio ruolo

Per utilizzare il servizio ruolo da script client, è necessario attivarlo utilizzando l'elemento seguente nel file Web.config dell'applicazione.

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

Per ulteriori informazioni, vedere Procedura: configurare servizi ASP.NET in ASP.NET AJAX.

Esempio

Nell'esempio seguente viene illustrato come utilizzare il servizio ruolo da script client.

Nota:

Prima di eseguire l'esempio, accertarsi che almeno un utente sia definito nel database di appartenenze dell'applicazione. Per informazioni sulla creazione di un utente nel database di SQL Server Express Edition predefinito, vedere Utilizzo dell'autenticazione basata su form con 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();

Vedere anche

Attività

Procedura: configurare servizi ASP.NET in ASP.NET AJAX

Concetti

Utilizzo dei servizi Web in ASP.NET AJAX

Chiamata a servizi Web da script client

Utilizzo dell'autenticazione basata su form con ASP.NET AJAX

Utilizzo delle informazioni sul profilo con ASP.NET AJAX

Classe Sys.Services.AuthenticationService

Classe Sys.Services.ProfileService

Classe Sys.Services.RoleService