Authorize Attribute with Multiple Roles
What i did is the answer in @Tieson
I tweak a little in his answer. Instead of string.Join why not convert it to list?
Here is my answer:
public class AuthorizeRolesAttribute : AuthorizeAttribute
{
private new List<string> Roles;
public AuthorizeRolesAttribute(params string[] roles) : base()
{
Roles = roles.toList()
}
}
And then check the if the role is valid overriding OnAuthorization
public override void OnAuthorization(HttpActionContext actionContext)
{
if (Roles == null)
HandleUnauthorizedRequest(actionContext);
else
{
ClaimsIdentity claimsIdentity = HttpContext.Current.User.Identity as ClaimsIdentity;
string _role = claimsIdentity.FindFirst(ClaimTypes.Role).Value;
bool isAuthorize = Roles.Any(role => role == _role);
if(!isAuthorize)
HandleUnauthorizedRequest(actionContext);
}
}
And there you have it, it is now validating if the role is authorized to access the resource
Try to create custom authorize attribute like this.
public class AuthorizeRolesAttribute : AuthorizeAttribute
{
public AuthorizeRolesAttribute(params string[] roles) : base()
{
Roles = string.Join(",", roles);
}
}
Assuming your roles will be the same for multiple controllers, create a helper class:
public static class Role
{
public const string Administrator = "Administrator";
public const string Assistant = "Assistant";
}
Then use it like so:
public class MyController : Controller
{
[AuthorizeRoles(Role.Administrator, Role.Assistant)]
public ActionResult AdminOrAssistant()
{
return View();
}
}
Make sure you are deriving your custom attribute class off System.Web.Mvc.AuthorizeAttribute
and NOT System.Web.Http.AuthorizeAttribute
.
I ran into the same problem. Once I changed it, everything worked.
You may also want to add the following to your custom attribute class:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
The best and simplest way I found to resolve this problem is just to concatenate roles in the Authorize attribute.
[Authorize(Roles = CustomRoles.Admin + "," + CustomRoles.OtherRole)]
with CustomRole a class with constant strings like this :
public static class CustomRoles
{
public const string Admin = "Admin";
// and so on..
}