Q:

Displaying content dynamically based on user role

My page hides content if the user isn't in a role that should see it.
Currently I have 2 roles that can see this content: global and admin.
If the user is in either of these roles, they should be able to see the content, but I'm having trouble working out the condition.
Here's what I've got so far:
// roles can be a single role ("global")
// or it can be multiple roles ("global,admin")
private bool CheckAllowed(string roles)
{
    var user = HttpContext.Current.User;
    var allowed = false;

    if (roles.Contains(","))
    {
        string[] rolesArr = roles.Split(',');

        foreach (string r in rolesArr)
        {
            allowed = (user.IsInRole(r)) ? true : false;
        }
    }
    else
    {
        allowed = (user.IsInRole(r)) ? true : false;
    }

    return allowed;
}

My issue is with the loop.
If the user is in rolesArr[0] but not in rolesArr[1] then the loop will mark them "not allowed" and they won't see the content they need to as a member of rolesArr[0].
How can I track whether or not a user is allowed to view the role-specific content more accurately?

A:

If the user is in rolesArr[0] but not in rolesArr[1] then the loop will mark them "not allowed" and they won't see the content they need to as a member of rolesArr[0].

Then what you're checking if it's user has any of those roles. There is a LINQ method for this:
bool allowed = rolesArr.Any(x => user.IsInRole(x));

In your code with:
foreach (string r in rolesArr)
    allowed = (user.IsInRole(r)) ? true : false;

You were just checking if user is in the last role of the list because each iteration will overwrite previous result (also note that this expression may be simplified to allowed = user.IsInRole(r)).

That said, note that you might simplify your code. First of all you do not need to check if string is a list or not, always Split() the string: performance impact is so negligible that it's not worth your effort. In short your code may be:
private bool CheckAllowed(string roles)
    => roles.Split(',').Any(x => HttpContext.Current.User.IsInRole(x));

Just for reference in case someone else will need it, if you need to check if user is in every role you can use All() instead of Any(). How it looks like without LINQ? Simply:
private bool CheckAllowed(string roles)
{
    foreach (var role in roles.Split(','))
    {
        if (HttpContext.Current.User.IsInRole(x))
            return true;
    }

    return false;
}

