Solved

Roles.IsUserInRole Multiple Roles?

Posted on 2009-04-14
1
1,796 Views
Last Modified: 2012-05-06
I'm currently using this for multiple Roles page login:

if (User.IsInRole("Administrators") | User.IsInRole("managers"))

I would like to use a shorter code like so, but not sure if its possible.

if (Roles.IsUserInRole("Administrators, managers"))
--or--
if (Roles.IsUserInRole("Administrators", "managers"))

I like using as little code as possible, when possible...

Thanks before hand..


0
Comment
Question by:pointeman
1 Comment
 
LVL 9

Accepted Solution

by:
tetorvik earned 250 total points
ID: 24145041
I believe you can compare only a single role at the time. If you need to do that comparision in multiple places, you may consider creating a helper method to shorten your code.
Something like:
public static bool IsUserInRoles(List<string> roles)
{
     return roles.Exists(role => Roles.IsUserInRole(role).Equals(true));
}
 
Or alternatively
 
public static bool IsUserInRoles(string roles)
{
     string[] arrRoles = roles.Split(',');
         foreach (string role in arrRoles)
              if (Roles.IsUserInRole(role))
                    return true;
         return false;
}
 
And use them like:
if (IsUserInRoles(new List<string>() { "Admin", "Manager", "User" }))
{ }
else if (IsUserInRoles("Admin,Manager,User"))
{ }

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question