Condividi tramite


ClientRoleProvider.GetRolesForUser(String) Metodo

Definizione

Ottiene i nomi dei ruoli ai quali appartiene l'utente specificato.

public:
 override cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public override string[] GetRolesForUser (string username);
override this.GetRolesForUser : string -> string[]
Public Overrides Function GetRolesForUser (username As String) As String()

Parametri

username
String

Nome dell'utente per il quale recuperare i ruoli.

Restituisce

String[]

Nomi del ruolo al quale appartiene username o matrice vuota se l'utente non è autenticato.

Eccezioni

username è Empty o null.

-oppure-

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

Esempio

Il codice di esempio seguente illustra come usare questo metodo per determinare se l'account di accesso dell'utente è scaduto prima di testare l'appartenenza al ruolo. Questo codice presuppone che tutti gli utenti validi siano associati a uno o più ruoli. In questo caso, il GetRolesForUser metodo non restituirà alcun ruolo per un utente autenticato in precedenza il cui account di accesso è scaduto. Se l'account di accesso dell'utente è scaduto, questo codice visualizza la finestra di dialogo di accesso. In caso contrario, chiama il IsUserInRole metodo per determinare se l'utente si trova nel ruolo "manager". Il codice con restrizioni si trova in 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

Il GetRolesForUser metodo recupera le informazioni sul ruolo per l'utente autenticato corrente, che è necessario specificare nel username parametro . È possibile ottenere il nome utente tramite la staticThread.CurrentPrincipal proprietà come indicato di seguito: Thread.CurrentPrincipal.Identity.Name.

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