ClientRolePrincipal.IsInRole(String) 메서드

정의

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

public:
 virtual bool IsInRole(System::String ^ role);
public bool IsInRole (string role);
abstract member IsInRole : string -> bool
override this.IsInRole : string -> bool
Public Function IsInRole (role As String) As Boolean

매개 변수

role
String

확인할 역할입니다.

반환

사용자가 지정된 역할에 속하면 true이고, 사용자가 지정된 역할에 속하지 않거나 인증되지 않았으면 false입니다.

구현

예제

다음 예제 코드에는 사용자가 "manager" 역할인 경우에 단추를 표시 하려면이 메서드를 사용 하는 방법을 보여 줍니다. 이 예제는 Button 라는 managerOnlyButton 초기값을 사용 하 여 Visible 속성 값 false합니다.

private void DisplayButtonForManagerRole()
{
    try
    {
        ClientRolePrincipal rolePrincipal =
            System.Threading.Thread.CurrentPrincipal 
            as ClientRolePrincipal;

        if (rolePrincipal != null && rolePrincipal.IsInRole("manager"))
        {
            managerOnlyButton.Visible = true;
        }
    }
    catch (System.Net.WebException)
    {
        MessageBox.Show("Unable to access the roles service.",
            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}
Private Sub DisplayButtonForManagerRole()

    Try

        Dim rolePrincipal As ClientRolePrincipal = TryCast( _
            System.Threading.Thread.CurrentPrincipal, ClientRolePrincipal)

        If rolePrincipal IsNot Nothing And _
            rolePrincipal.IsInRole("manager") Then

            managerOnlyButton.Visible = True

        End If

    Catch ex As System.Net.WebException

        MessageBox.Show("Unable to access the role service.", _
            "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    End Try

End Sub

설명

일반적으로 호출 하는 IsInRole 메서드를 IPrincipal 반환한를 staticThread.CurrentPrincipal 속성. 캐스팅할 수 있습니다는 CurrentPrincipal 속성 값을 ClientRolePrincipal 예제 단원에 설명 된 대로 명시적으로이 메서드를 호출에 대 한 참조입니다.

IsInRole 메서드는 사용자 로그인이 만료된 경우 항상 false 를 반환합니다. 애플리케이션을 호출 하는 경우 발생 하지 것입니다는 IsInRole 메서드를 한 번 인증 직후입니다. 다른 시간에 애플리케이션이 사용자 역할을 검색해야 하는 경우 해당 로그인이 만료된 사용자의 유효성을 다시 검사하는 코드를 추가하는 것이 좋습니다. 모든 유효한 사용자가 역할에 할당되면 ClientRoleProvider.GetRolesForUser 메서드를 호출하여 로그인이 만료되었는지 여부를 확인할 수 있습니다. 역할이 반환되지 않는 경우 로그인이 만료된 것입니다. 이 기능의 예는 GetRolesForUser 메서드를 참조하세요. 이 기능은 애플리케이션 구성에서 서버 쿠키가 만료될 때마다 다시 사용자 로그온을 선택한 경우에만 필요합니다.

적용 대상

추가 정보