Solved

Roles.IsUserInRole Multiple Roles?

Posted on 2009-04-14
1
1,727 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now