Roles.IsUserInRole Method (String)

Gets a value indicating whether the currently logged-on user is in the specified role.

Namespace: System.Web.Security
Assembly: System.Web (in system.web.dll)

public static bool IsUserInRole (
	string roleName
)
public static boolean IsUserInRole (
	String roleName
)
public static function IsUserInRole (
	roleName : String
) : boolean
Not applicable.

Parameters

roleName

The name of the role to search in.

Return Value

true if the currently logged-on user is in the specified role; otherwise, false.

Exception typeCondition

System.ArgumentNullException

roleName is a null reference (Nothing in Visual Basic).

-or-

There is no current logged-on user.

System.ArgumentException

roleName is an empty string or contains a comma (,).

System.Configuration.Provider.ProviderException

Role management is not enabled.

The IsUserInRole method calls the RoleProvider.IsUserInRole method of the default role provider to determine whether the currently logged-on user is associated with a role from the data source for the application that is specified in the ApplicationName property. The currently logged-on user is identified by the System.Web.HttpContext.User property of the current System.Web.HttpContext, or by Thread.CurrentPrincipal for non-HTTP hosting environments. If no user is logged on, an exception will be thrown. Only the roles for the application that is specified in the ApplicationName property are retrieved.

If CacheRolesInCookie is true, then roleName may be checked against the roles cache rather than the specified role provider.

The following code example programmatically checks whether the currently logged-on user is in the Administrators role before allowing the user to view roles settings for the application. For an example of a Web.config file that enables role management, see Roles.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show: