Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Roles Class
Roles Methods
 IsUserInRole Method (String)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Shared Function IsUserInRole ( _
    roleName As String _
) As Boolean
Visual Basic (Usage)
Dim roleName As String
Dim returnValue As Boolean

returnValue = Roles.IsUserInRole(roleName)
C#
public static bool IsUserInRole(
    string roleName
)
Visual C++
public:
static bool IsUserInRole(
    String^ roleName
)
JScript
public static function IsUserInRole(
    roleName : String
) : boolean

Parameters

roleName
Type: System..::.String
The name of the role to search in.

Return Value

Type: System..::.Boolean
true if the currently logged-on user is in the specified role; otherwise, false.
ExceptionCondition
System..::.ArgumentNullException

roleName is nullNothingnullptra 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 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker