ASP.NET MVC: Opposite of [Authorise]
I tried creating my own AuthorizationAttribute after twk's suggestion:
public class Restrict : AuthorizeAttribute
{
private readonly string _role;
public Restrict(string role)
{
_role = role;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
if (httpContext.User.IsInRole(_role))
return false;
return true;
}
}
And I use it like this:
[Restrict("Administrator")]
public class HomeController : Controller
{
// code
}
I'm unsure whether it is correct practice but it does the job.