Share via


ClientRoleProvider.GetRolesForUser(String) 메서드

정의

지정된 사용자가 속하는 역할의 이름을 가져옵니다.

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

매개 변수

username
String

역할을 검색할 사용자의 이름입니다.

반환

String[]

username이 속하는 역할 이름이거나 사용자가 인증되지 않은 경우 빈 배열입니다.

예외

usernameEmpty 또는 null입니다.

또는

username이 현재 인증된 사용자의 사용자 이름이 아닌 경우

예제

다음 예제 코드에서는이 메서드를 사용 하 여 사용자 로그인 역할 멤버 자격을 테스트 하기 전에 만료 되었는지 여부를 결정 하는 방법을 보여 줍니다. 이 코드는 모든 유효한 사용자가 연결 된 하나 이상의 역할을 가정 합니다. 이 경우에 GetRolesForUser 메서드는 해당 로그인이 만료 이전에 인증 된 사용자에 대 한 모든 역할을 반환 하지 것입니다. 사용자 로그인이 만료 된 경우이 코드는 로그인 대화 상자를 표시 합니다. 를 호출 합니다 IsUserInRole 사용자 "manager" 역할에 있는지 확인 하는 방법입니다. 제한 된 코드는 PerformManagerTask 메서드를 제공 하지 않습니다.

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

설명

합니다 GetRolesForUser 에 지정 해야 하는 현재, 인증 된 사용자에 대 한 역할 정보를 검색 하는 메서드는 username 매개 변수입니다. 사용자 이름을 가져올 수 있습니다 합니다 staticThread.CurrentPrincipal 속성을 다음과 같이: Thread.CurrentPrincipal.Identity.Name합니다.

서비스 공급자는 불필요 한 서비스 호출을 방지 하려면 로컬 파일 시스템에 대 한 역할 정보를 캐시 합니다. 자세한 내용은 참조는 ClientRoleProvider 클래스 개요입니다.

적용 대상

추가 정보