Solved

Roles.IsUserInRole Multiple Roles?

Posted on 2009-04-14
1
1,840 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

Industry Leaders: 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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