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 が、現在の認証されたユーザー名ではありません。

次のコード例では、このメソッドを使用して、ロール メンバーシップをテストする前にユーザー ログインの有効期限が切れているかどうかを確認する方法を示します。 このコードでは、すべての有効なユーザーが 1 つ以上のロールに関連付けられていることを前提としています。 この場合、 メソッドは、 GetRolesForUser ログインの有効期限が切れた以前に認証されたユーザーのロールを返しません。 ユーザー ログインの有効期限が切れている場合、このコードはログイン ダイアログ ボックスを表示します。 それ以外の場合は、 メソッドを 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

注釈

メソッドは GetRolesForUser 、パラメーターで指定する必要がある、現在の認証済みユーザーのロール情報を username 取得します。 ユーザー名は、 プロパティをstaticThread.CurrentPrincipal使用して取得できます。 Thread.CurrentPrincipal.Identity.Name

サービス プロバイダーは、不要なサービス呼び出しを回避するために、ローカル ファイル システムに関するロール情報をキャッシュします。 詳細については、クラスの概要に関するページを ClientRoleProvider 参照してください。

適用対象

こちらもご覧ください