共用方式為


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

例外狀況

usernameEmptynull

-或-

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

備註

您可以藉由呼叫 IsInRole 屬性所staticThread.CurrentPrincipal傳回的 IPrincipal 方法,判斷已驗證的使用者是否為特定角色。 針對設定為使用用戶端應用程式服務的應用程式,這個屬性會傳回 ClientRolePrincipal。 因為這個類別會實作 IPrincipal 介面,所以您不需要明確地參考它。 方法 ClientRolePrincipal.IsInRole 會在內部呼叫 IsUserInRole 方法。 方法IsUserInRoleGetRolesForUser使用 方法來判斷 所指示的使用者是否在 所roleName指示username的角色中。

服務提供者會快取本機文件系統的相關角色資訊,以避免不必要的服務呼叫。 如需詳細資訊,請參閱 ClientRoleProvider 類別概觀。

適用於

另請參閱