Export (0) Print
Expand All

ClientRoleProvider.IsUserInRole Method

Gets a value indicating whether the specified user is in the specified role.

Namespace:  System.Web.ClientServices.Providers
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public override bool IsUserInRole(
	string username,
	string roleName
)

Parameters

username
Type: System.String

The name of the user.

roleName
Type: System.String

The name of the role.

Return Value

Type: System.Boolean
true if the specified user is in the specified role; false if the specified user is not authenticated or is not in the specified role.

ExceptionCondition
ArgumentException

username is Empty or null.

-or-

username is not the user name of the current, authenticated user.

WebException

The user is no longer authenticated.

-or-

The roles service is not available.

You can determine whether an authenticated user is in a particular role by calling the IsInRole method of the IPrincipal returned by the static Thread.CurrentPrincipal property. For applications configured to use client application services, this property returns a ClientRolePrincipal. Because this class implements the IPrincipal interface, you do not need to reference it explicitly. The ClientRolePrincipal.IsInRole method internally calls the IsUserInRole method. The IsUserInRole method uses the GetRolesForUser method to determine whether the user indicated by username is in the role indicated by roleName.

The service provider caches role information about the local file system to avoid unnecessary service calls. For more information, see the ClientRoleProvider class overview.

The following example code demonstrates how to access this method directly to determine whether the user is in a particular role. This code first tests whether the user login has expired. An explicit ClientRoleProvider reference is required to call the GetRolesForUser method, so the same reference is used to call the IsUserInRole method. If the user is in the "manager" role, this code calls 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);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft