Share via


Verwenden von Rolleninformationen mit ASP.NET-AJAX

Aktualisiert: November 2007

Mit ASP.NET-Rollen können Sie authentifizierte Benutzer gruppieren und jede Gruppe als Einheit behandeln. Dies kann bei Aktionen, die für mehrere Benutzer gelten, wie beispielsweise das Gewähren oder Verweigern von Zugriff auf Ressourcen des Servers, nützlich sein.

Der Microsoft AJAX Library-Anwendungsrollendienst ermöglicht Ihnen den Zugriff auf die Rollen eines authentifizierten Benutzers von Clientskript in einer AJAX-fähigen ASP.NET-Webanwendung. Sie können den Microsoft AJAX Library-Anwendungsrollendienst verwenden, um rollenbasierte Aufgaben unter Verwendung von Clientskript auszuführen. Bei der Ausführung der Seite können Sie die ECMAScript (JavaScript)-RoleService-Proxyklasse aufrufen, die automatisch vom Anwendungsrollendienst generiert und in den Browser heruntergeladen wird.

In diesem Thema wird erläutert, wie der ASP.NET-Anwendungsrollendienst im Browser mithilfe von JavaScript aufgerufen wird.

Aktivieren des Rollendiensts

Zur Verwendung des Rollendiensts aus dem Clientskript müssen Sie den Rollendienst aktivieren, indem Sie das folgende Element in der Datei Web.config der Anwendung verwenden.

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

Weitere Informationen finden Sie unter Gewusst wie: Konfigurieren von ASP.NET-Diensten in ASP.NET AJAX.

Beispiel

Das folgende Beispiel zeigt, wie der Rollendienst vom Clientskript verwendet wird.

Hinweis:

Stellen Sie zum Ausführen des Beispiels sicher, dass in der Mitgliedschaftsdatenbank der Anwendung mindestens ein Benutzer definiert ist. Informationen zum Erstellen eines Benutzers in der Standarddatenbank von SQL Server Express Edition finden Sie unter Verwenden der Formularauthentifizierung mit 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();

Siehe auch

Aufgaben

Gewusst wie: Konfigurieren von ASP.NET-Diensten in ASP.NET AJAX

Konzepte

Verwenden von Webdiensten in ASP.NET-AJAX

Aufrufen von Webdiensten vom Clientskript

Verwenden der Formularauthentifizierung mit ASP.NET-AJAX

Verwenden von Profilinformationen mit ASP.NET-AJAX

Sys.Services.AuthenticationService-Klasse

Sys.Services.ProfileService-Klasse

Sys.Services.RoleService-Klasse