ClientRoleProvider.GetRolesForUser(String) 方法

定义

获取指定用户所属的角色的名称。Gets the names of the roles that the specified user belongs to.

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

要检索其角色的用户的名称。The name of the user to retrieve roles for.

返回

String[]

username 所属的角色名称;如果用户未通过身份验证,则为一个空数组。The role names that username belongs to or an empty array if the user is not authenticated.

例外

usernameEmptynullusername is Empty or null.

- 或 --or- username 不是当前通过身份验证的用户的用户名。username is not the user name of the current, authenticated user.

示例

下面的示例代码演示如何使用此方法在测试角色成员资格之前确定用户登录名是否已过期。The following example code demonstrates how to use this method to determine whether the user login has expired before testing role membership. 此代码假定所有有效用户都与一个或多个角色关联。This code assumes that all valid users are associated with one or more roles. 在这种情况下,该 GetRolesForUser 方法将不会为其登录名已过期的以前经过身份验证的用户返回任何角色。In this case, the GetRolesForUser method will not return any roles for a previously-authenticated user whose login has expired. 如果用户登录名已过期,则此代码将显示登录对话框。If the user login has expired, this code displays the login dialog box. 否则,它会调用 IsUserInRole 方法来确定用户是否为 "经理" 角色。Otherwise, it calls the IsUserInRole method to determine whether the user is in the "manager" role. 受限代码位于 PerformManagerTask 方法中,但未提供此方法。The restricted code is in a PerformManagerTask method, which is not provided.

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方法检索当前经过身份验证的用户的角色信息,必须在参数中指定该信息 usernameThe GetRolesForUser method retrieves role information for the current, authenticated user, which you must specify in the username parameter. 可以通过属性获取用户名称 static Thread.CurrentPrincipal ,如下所示: Thread.CurrentPrincipal.Identity.NameYou can get the user name through the static Thread.CurrentPrincipal property as follows: Thread.CurrentPrincipal.Identity.Name.

服务提供程序会缓存有关本地文件系统的角色信息,以避免不必要的服务调用。The service provider caches role information about the local file system to avoid unnecessary service calls. 有关详细信息,请参阅 ClientRoleProvider 类概述。For more information, see the ClientRoleProvider class overview.

适用于

另请参阅