다음을 통해 공유


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입니다.

예외

usernameEmpty 또는 null입니다.

또는

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

사용자가 더 이상 인증된 상태가 아닌 경우

또는

역할 서비스를 사용할 수 없는 경우

예제

다음 예제 코드에 직접 사용자가 특정 역할에 있는지 여부를 확인 하려면이 메서드를 액세스 하는 방법을 보여 줍니다. 이 코드는 먼저 사용자 로그인이 만료 여부를 테스트 합니다. 명시적인 ClientRoleProvider 참조를 호출 해야 합니다 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

설명

호출 하 여 인증된 된 사용자가 특정 역할에 있는지 여부를 확인할 수 있습니다는 IsInRole 메서드를 IPrincipal 반환한 합니다 staticThread.CurrentPrincipal 속성. 클라이언트 애플리케이션 서비스를 사용하도록 구성된 애플리케이션에 대해 이 속성은 ClientRolePrincipal을 반환합니다. 이 클래스는 IPrincipal 인터페이스를 구현하기 때문에 명시적으로 참조할 필요가 없습니다. 합니다 ClientRolePrincipal.IsInRole 내부적으로 호출 된 IsUserInRole 메서드. IsUserInRole 메서드를 GetRolesForUser 사용자에 나타난 여부를 결정 하는 방법 username 으로 표시 된 역할에 roleName합니다.

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

적용 대상

추가 정보