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 の初期Visibleプロパティ値を持つ という名前managerOnlyButtonfalse必要です。

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

注釈

通常は、 プロパティによって返される の メソッドをIPrincipalstaticThread.CurrentPrincipal呼び出IsInRoleします。 ただし、「例」セクションに CurrentPrincipal 示すように、プロパティ値を ClientRolePrincipal 参照にキャストして、このメソッドを明示的に呼び出すことができます。

ユーザー ログインの有効期限が切れている場合、 IsInRole メソッドは常に false を返します。 これは、アプリケーションが認証の直後にメソッドを IsInRole 1 回呼び出した場合には発生しません。 アプリケーションが他のタイミングでユーザーのロールを取得する必要がある場合は、ログインの有効期限が切れたユーザーを再検証するコードを追加することができます。 有効なユーザーすべてにロールが割り当てられている場合は、 ClientRoleProvider.GetRolesForUser メソッドを呼び出してログインの有効期限が切れていないか判断できます。 ロールが返されない場合は、ログインの有効期限が切れています。 この機能の例については、 GetRolesForUser メソッドを参照してください。 この機能が必要なのは、アプリケーションの構成で [サーバー クッキーの期限が切れた場合は常に再度ログオンすることをユーザーに要求する] を選択した場合だけです。

適用対象

こちらもご覧ください