Taal

ClientRoleProvider.GetRolesForUser(String) Methode

Definitie

Hiermee haalt u de namen op van de rollen waartoe de opgegeven gebruiker behoort.

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()

Parameters

username
String

De naam van de gebruiker voor het ophalen van rollen.

Retouren

String[]

De rolnamen die username deel uitmaken van of een lege matrix als de gebruiker niet is geverifieerd.

Uitzonderingen

username is Empty of null.

– of –

username is niet de gebruikersnaam van de huidige geverifieerde gebruiker.

Voorbeelden

In de volgende voorbeeldcode ziet u hoe u deze methode gebruikt om te bepalen of de aanmelding van de gebruiker is verlopen voordat u het rollidmaatschap test. Bij deze code wordt ervan uitgegaan dat alle geldige gebruikers zijn gekoppeld aan een of meer rollen. In dit geval retourneert de methode geen rollen voor een eerder geverifieerde gebruiker waarvan de GetRolesForUser aanmelding is verlopen. Als de aanmelding van de gebruiker is verlopen, wordt met deze code het aanmeldingsdialoogvenster weergegeven. Anders wordt de IsUserInRole methode aangeroepen om te bepalen of de gebruiker de rol Manager heeft. De beperkte code bevindt zich in een PerformManagerTask methode die niet is opgegeven.

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

Opmerkingen

De GetRolesForUser methode haalt rolgegevens op voor de huidige, geverifieerde gebruiker, die u in de username parameter moet opgeven. U kunt de gebruikersnaam als volgt ophalen via de staticThread.CurrentPrincipal eigenschap: Thread.CurrentPrincipal.Identity.Name

De serviceprovider slaat rolgegevens over het lokale bestandssysteem in de cache op om onnodige service-aanroepen te voorkomen. Zie het ClientRoleProvider klasoverzicht voor meer informatie.

Van toepassing op

Zie ook