0 out of 2 rated this helpful - Rate this topic

IPrincipal.IsInRole Method

Determines whether the current principal belongs to the specified role.

Namespace:  System.Security.Principal
Assembly:  mscorlib (in mscorlib.dll)
bool IsInRole(
	string role
)

Parameters

role
Type: System.String
The name of the role for which to check membership.

Return Value

Type: System.Boolean
true if the current principal is a member of the specified role; otherwise, false.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Extended IsInRole(string,string, ...) or IsInRole(csvString)
It is often needed to check if a user is in more than one role to grant access to a feature. $0It would be nice to have a new version of this method accepting a comma separated list of roles, of a string list of roles. The method would return true if the user is in one of these roles.$0 $0$0 $0 $0Better, it would be nice to have a built in parser for simple combination: IsInRole("((role1 && role2) || role3 && !role4)")$0 $0$0 $0
Response to suggestion
I had some immediate success with the built-in groups, for example
    HttpContext.Current.User.IsInRole(@"BUILTIN\Administrators")

There is a real gotcha with local groups when you add a new user. You really have to log out and log back in again before IsInRole will return true.

Once this was done I had success with local groups on my development PC using the format
    IsInRole("MyPCName\MyLocalGroupName")
and also
   IsInRole("MyLocalGroupName")

Suggestion
It would be very helpful if this documentation included more information on what the role string needs to be, what typical role names are in Windows, etc.