Condividi tramite


ClientRoleProvider.IsUserInRole(String, String) Metodo

Definizione

Ottiene un valore che indica se l'utente specificato appartiene al ruolo specificato.

public:
 override bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public override bool IsUserInRole (string username, string roleName);
override this.IsUserInRole : string * string -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean

Parametri

username
String

Il nome dell'utente.

roleName
String

Nome ruolo.

Restituisce

true se l'utente specificato è nel ruolo specificato; false se l'utente specificato non è autenticato o non è nel ruolo specificato.

Eccezioni

username è Empty o null.

-oppure-

username non è il nome utente dell'utente corrente autenticato.

L'utente non è più autenticato.

-oppure-

Il servizio ruoli non è disponibile.

Esempio

Il codice di esempio seguente illustra come accedere direttamente a questo metodo per determinare se l'utente si trova in un ruolo specifico. Questo codice verifica prima se l'account di accesso dell'utente è scaduto. Per chiamare il GetRolesForUser metodo è necessario un riferimento esplicitoClientRoleProvider, quindi lo stesso riferimento viene usato per chiamare il IsUserInRole metodo . Se l'utente è nel ruolo "manager", questo codice chiama un PerformManagerTask metodo che non viene fornito.

private void AttemptManagerTask()
{
    System.Security.Principal.IIdentity identity =
        System.Threading.Thread.CurrentPrincipal.Identity;

    // Return if the authentication type is not "ClientForms". 
    // This indicates that the user is logged out.
    if (!identity.AuthenticationType.Equals("ClientForms")) return;

    try
    {
        ClientRoleProvider provider =
            (ClientRoleProvider)System.Web.Security.Roles.Provider;
        String userName = identity.Name;

        // Determine whether the user login has expired by attempting
        // to retrieve roles from the service. Call the ResetCache method
        // to ensure that the roles are retrieved from the service. If no 
        // roles are returned, then the login has expired. This assumes 
        // that every valid user has been assigned to one or more roles.
        provider.ResetCache();
        String[] roles = provider.GetRolesForUser(userName);
        if (roles.Length == 0)
        {
            MessageBox.Show(
                "Your login has expired. Please log in again to access " +
                "the roles service.", "Attempting to access user roles...");

            // Call ValidateUser with empty strings in order to 
            // display the login dialog box configured as a 
            // credentials provider.
            if (!System.Web.Security.Membership.ValidateUser(
                String.Empty, String.Empty))
            {
                MessageBox.Show("Unable to authenticate. " +
                    "Cannot retrieve user roles.", "Not logged in",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        if (provider.IsUserInRole(userName, "manager"))
        {
            PerformManagerTask();
        }
    }
    catch (System.Net.WebException)
    {
        MessageBox.Show(
            "Unable to access the remote service. " +
            "Cannot retrieve user roles.", "Warning",
            MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}
Private Sub AttemptManagerTask()

    Dim identity As System.Security.Principal.IIdentity = _
        System.Threading.Thread.CurrentPrincipal.Identity

    ' Return if the authentication type is not "ClientForms". 
    ' This indicates that the user is logged out.
    If Not identity.AuthenticationType.Equals("ClientForms") Then Return

    Try

        Dim provider As ClientRoleProvider = _
            CType(System.Web.Security.Roles.Provider, ClientRoleProvider)
        Dim userName As String = identity.Name

        ' Determine whether the user login has expired by attempting
        ' to retrieve roles from the service. Call the ResetCache method
        ' to ensure that the roles are retrieved from the service. If no 
        ' roles are returned, then the login has expired. This assumes 
        ' that every valid user has been assigned to one or more roles.
        provider.ResetCache()
        Dim roles As String() = provider.GetRolesForUser(userName)
        If roles.Length = 0 Then

            MessageBox.Show( _
                "Your login has expired. Please log in again to access " & _
                "the roles service.", "Attempting to access user roles...")

            ' Call ValidateUser with empty strings in order to 
            ' display the login dialog box configured as a 
            ' credentials provider.
            If Not System.Web.Security.Membership.ValidateUser( _
                String.Empty, String.Empty) Then

                MessageBox.Show("Unable to authenticate. " & _
                    "Cannot retrieve user roles.", "Not logged in", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return

            End If

        End If

        If provider.IsUserInRole(userName, "manager") Then
            PerformManagerTask()
        End If

    Catch ex As System.Net.WebException

        MessageBox.Show( _
            "Unable to access the remote service. " & _
            "Cannot retrieve user roles.", "Warning", _
            MessageBoxButtons.OK, MessageBoxIcon.Warning)

    End Try

End Sub

Commenti

È possibile determinare se un utente autenticato si trova in un determinato ruolo chiamando il IsInRole metodo dell'oggetto IPrincipal restituito dalla staticThread.CurrentPrincipal proprietà . Per le applicazioni configurate per l'uso dei servizi delle applicazioni client, questa proprietà restituisce un oggetto ClientRolePrincipal. Poiché questa classe implementa l'interfaccia IPrincipal , non è necessario farvi riferimento in modo esplicito. Il ClientRolePrincipal.IsInRole metodo chiama internamente il IsUserInRole metodo . Il IsUserInRole metodo usa il GetRolesForUser metodo per determinare se l'utente indicato da username è nel ruolo indicato da roleName.

Il provider di servizi memorizza nella cache le informazioni sul file system locale per evitare chiamate di servizio non necessarie. Per altre informazioni, vedere la panoramica della ClientRoleProvider classe.

Si applica a

Vedi anche