다음을 통해 공유


ClientRoleProvider.IsUserInRole(String, String) 메서드

정의

지정된 사용자가 지정된 역할에 있는지 여부를 나타내는 값을 가져옵니다.

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

매개 변수

username
String

사용자의 이름입니다.

roleName
String

역할의 이름입니다.

반환

지정된 사용자가 지정된 역할에 있는지 true. 지정된 사용자가 인증되지 않았거나 지정된 역할에 있지 않은 경우 false.

예외

username Empty 또는 null.

-또는-

username 인증된 현재 사용자의 사용자 이름이 아닙니다.

사용자가 더 이상 인증되지 않습니다.

-또는-

역할 서비스를 사용할 수 없습니다.

예제

다음 예제 코드는 이 메서드에 직접 액세스하여 사용자가 특정 역할에 있는지 여부를 확인하는 방법을 보여 줍니다. 이 코드는 먼저 사용자 로그인이 만료되었는지 여부를 테스트합니다. GetRolesForUser 메서드를 호출하려면 명시적 ClientRoleProvider 참조가 필요하므로 IsUserInRole 메서드를 호출하는 데 동일한 참조가 사용됩니다. 사용자가 "관리자" 역할에 있는 경우 이 코드는 제공되지 않는 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

설명

static Thread.CurrentPrincipal 속성에서 반환된 IPrincipalIsInRole 메서드를 호출하여 인증된 사용자가 특정 역할에 있는지 여부를 확인할 수 있습니다. 클라이언트 애플리케이션 서비스를 사용하도록 구성된 애플리케이션의 경우 이 속성은 ClientRolePrincipal반환합니다. 이 클래스는 IPrincipal 인터페이스를 구현하므로 명시적으로 참조할 필요가 없습니다. ClientRolePrincipal.IsInRole 메서드는 내부적으로 IsUserInRole 메서드를 호출합니다. IsUserInRole 메서드는 GetRolesForUser 메서드를 사용하여 username 표시된 사용자가 roleName표시되는 역할에 있는지 여부를 확인합니다.

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

적용 대상

추가 정보