Solved

Roles.IsUserInRole Multiple Roles?

Posted on 2009-04-14
1
1,906 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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