How can I check if a user is in any one of a few different roles with MVC4 Simple membership?

There’s no built-in way to check if a user is in multiple roles, but it’s pretty trivial to create a nice extension method to handle it for you:

public static bool IsInAnyRole(this IPrincipal principal, params string[] roles)
{
    return roles.Any(principal.IsInRole);
}

Usage then is:

if (User.IsInAnyRole("Admin", "Author", "SuperUser"))
{

}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)