Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Roles.IsUserInRole Multiple Roles?

Posted on 2009-04-14
1
Medium Priority
?
1,970 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 1000 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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

664 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