Ok, here it is. My understanding of your problem is that you want to check if the user has one or more specific roles. If yes, allow access; otherwise, deny them access and send them into a black hole. In your example, it sounds like you only want to test one specific role. By reading some of your comments, you can also check out several roles at some point. The sample code that I will show you allows you to check the user for a set of one or more roles that the user must be assigned to access the page.
string[] requiredRoles = new string[] { "Awesome", "Pancake" }; if (requiredRoles.Except(theUsersAssignedRoles).Any()) {
The idea here is to take a list of all the required roles and subtract all the roles that the user has assigned to him or her. If after subtraction there are any roles remaining in the list, then the user does not have all the necessary roles. Here are some examples. The roles that are required for authorization are to the left of the subtraction sign, while the roles assigned by the user are to the right of the subtraction sign, as is the above code.
The user is amazing and damn, so they are allowed access. Note that we end up with an empty array, which means that the user meets the requirements:
{ "Awesome", "Pancake" } - { "Awesome", "Pancake" } = { }
The user is just a pancake. They are not allowed access. The resulting set contains "Awesome", so it means that they lack the role of "Awesome":
{ "Awesome", "Pancake" } - { "Pancake" } = { "Awesome" }
This user is not amazing or damn, but she is the admin. However, entering the page requires amazing pancakes so that this user is denied access:
{ "Awesome", "Pancake" } - { "Admin" } = { "Awesome", "Pancake" }
If you have any questions or this is not what you want, leave a comment. I will be happy to help you if necessary.