IPrincipal.IsInRole Method
.NET Framework 4
Determines whether the current principal belongs to the specified role.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- role
- Type: System.String
The name of the role for which to check membership.
Return Value
Type: System.Booleantrue if the current principal is a member of the specified role; otherwise, false.
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.
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
- 6/10/2011
- Softlion
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")
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")
- 3/22/2011
- Mike Wiese
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.
- 12/2/2010
- Bradley.Ward